简体   繁体   中英

Python nested loops for renaming files

I would like to rename a file by using os.rename() like this:

x_y.jpg 

x should come from a list ['2', '3', '4', '5', '8', '9', '10'] .

y should count from 1 to 10 . After it reaches 10 , x should jump to the second number on the list (here: '3' ) and y should start again with counting. Like this:

2_1.jpg
2_2.jpg
2_3.jpg
...
3_1.jpg
3_2.jpg

I think the best thing to do so ist to built a nested loop, but I am not sure how, because I am a beginner with python.I thought about somthing like this, but this does not work properly. Only the first file is renamed. Then an Error saying that the file is already existing comes.

my_list = ['2', '3', '4', '5', '8', '9', '10']
included_extensions = ['jpg', 'JPG']
directory = [fn for fn in os.listdir(source_folder)
              if any(fn.endswith(ext) for ext in included_extensions)]

for y, file in enumerate(directory):
    for x in range(10):
        for x in my_list:
            os.rename(os.path.join(source_folder,file), os.path.join(destination_folder, ''.join([str(x),'_',str(y+1),'.jpg'])))

You should only need one for loop. You only want to rename each file once, and hence you should only need to iterate through the file names once.

To iterate through the different name types you want, you could use the index from the enumeration instead. For instance - using

for z, file in enumerate(directory):

every time a new file is reached, your 'x' and 'y' values could be evaluated like

this_x = my_list[z // 10]
this_y = z % 10

to end up with something like


for z, file in enumerate(directory):
    newName = str(my_list[z // 10]) + '_' + str(z % 10) + '.jpg'
    os.rename(os.path.join(source_folder,file), os.path.join(destination_folder, newName))
    fileCount += 1

You are overwriting x . Also your loops are not doing the thing they need to do. By looping through the files in the first loop, your inner loop will have the same filename for each run, therefore after the first run, the file will be renamed, and the second run cannot access it anymore.

for y, file in enumerate(directory):
    for x in range(10):    # first  x
        for x in my_list:  # second x
            os.rename(os.path.join(source_folder,file), os.path.join(destination_folder, ''.join([str(x),'_',str(y+1),'.jpg'])))

Also, using nested loops is an ok idea, but then you need to do

i = 0
for y in my_list:
    for x in range(10):
        os.rename(os.path.join(source_folder, directory[i]), os.path.join(destination_folder, '{}_{}.jpg'.format(y, x)))
        i += 1

To jump y , when x gets to 10. I would define an i variable to track which file comes next, and do i += 1 , after each rename, and this way you may need an exit condition, when the directory list gets to its end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM