2

I have this large directory of 1000+ files from a machine learning dataset, but these files have different qualities (pictures of roses vs. daises to keep it simple). I have this CSV file with the filenames of each of these items in the dataset as well as their categorization (roses vs. daisies). How can I read this CSV file and tell my file manager to move all of the rose photos to one directory and all of the daisy photos to another directory? Would I need to use a Bash script, or is this something that's already built into Nautilus?

0

Alright, a friend and I managed to write a script in Python that solved this quite well.

# Import csv
import csv
# Import os
import os

# Main Function
def main():
# Open dataset file
dataset = open('dataset.csv', newline='')

# Initialize csvreader for dataset
reader = csv.reader(dataset)

# Read data from reader
data = list(reader)

# Variables for progress counter
lines = len(data)
i = 0

# Analyze data in dataset
for row in data:
    # Assign image name and state to variables
    image = row[0] + '.jpeg'
    state = row[1]

    # Print image information
    print('({}/{}) Processing image ({}): {}'.format(i + 1, lines, state, image))

    # Increment i
    i += 1

    # Determine action to perform
    if state is '0':
        # Attempt to move the file
        try:
            # Move the file to nosymptoms/
            os.rename(image, 'nosymptoms/' + image)
            # Inform the user of action being taken
            print(' -> Moved to nosymptoms/')
        except FileNotFoundError:
            # Inform the user of the failure
            print(' -> Failed to find file')
    elif state in ['1', '2', '3', '4']:
        # Attempt to move the file
        try:
            # Move the file to nosymptoms/
            os.rename(image, 'symptoms/' + image)
            # Inform the user of action being taken
            print(' -> Moved to symptoms/')
        except FileNotFoundError:
            # Inform the user of the failure
            print(' -> Failed to find file')

# Execute main function if name is equal to main
if __name__ == '__main__':
main()

This tended to work better because I had more categories to deal with...hopefully this will work for anyone with the same problem.

0

Here is a bash script that should do what you want:

#!/bin/bash

fileNameIndex=0   # set to index of file name
categoryIndex=1   # set to index of category

IFS=",""$IFS"     # add comma to break lines at commas

while read -a tokens;    # read a line and break it into tokens separated by commas
do
    file=${tokens[$fileNameIndex]}       # get the file name
    category=${tokens[$categoryIndex]}  # get the category
    if [ ! -d $category ]; then         # check if directory with category name exists
        mkdir $category;                # make the category directory
    fi
    mv $file $category                  # move the file into the category directory
done

Save this script in a file, perhaps do_moves.sh, edit it to set the correct values for fileNameIndex and categoryIndex, then run it as:

./do_moves.sh < data.csv

where data.csv is your CSV file. Make sure that you do not have any files that have the same name as a category before you run this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.