Skip to content

Instantly share code, notes, and snippets.

@javathunderman
Created March 5, 2017 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javathunderman/6a9f9c8a0ee29cb7a817777421685e49 to your computer and use it in GitHub Desktop.
Save javathunderman/6a9f9c8a0ee29cb7a817777421685e49 to your computer and use it in GitHub Desktop.
Moves files around based off of a CSV file
# 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 folderone/
os.rename(image, 'folderone/' + image)
# Inform the user of action being taken
print(' -> Moved to folderone/')
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 foldertwo/
os.rename(image, 'foldertwo/' + image)
# Inform the user of action being taken
print(' -> Moved to foldertwo/')
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment