I have over 1000 folders in 'my documents'. In each folder there are only 4 photos that need to be named to 'North' 'East' 'South' and 'West' in that order. Currently they are named DSC_XXXX. I have written this script but it is not executing.
import sys, os
folder_list = []
old_file_list = []
newnames = [North, South, East, West]
for file in os.listdir(sys.argv[1]):
folder_list.append(file)
for i in range(len(folder_list)):
file = open(folder_list[i], r+)
for file in os.listdir(sys.argv[1] + '/' folder_list[i]):
old_file_list.append(file)
os.rename(old_file_list, newnames[i])
My method of thinking was to get all the names of the 1000 folders and store them in folder_list. Then open each folder and save the 4 picture names in old_file_list. From there I would like to use the os.rename(old_file_list, newnames[i]) to rename the 4 photos to North East South and West. Then I want this to loop for as many folders that are in 'my documents'. I am new to python and any help or suggestions would be greatly appreciated.
os.listdirreturns an arbitrarily-orderedlist. Also, you can iterate directly overfolder_listinstead of usingrange(len())to work with indices.os.listdir()already returns alist, so you don't need to iterate over it and build a new one. Just save a reference to it:folder_list = os.listdir(sys.argv[1]).open()folders... although you're masking it with the loop variable anyway.