简体   繁体   English

使用包含不同字符串的名称列表重命名文件

[英]renaming files with list of names that's contains different strings

I have a folder of images like this我有一个像这样的图像文件夹

img (1).png
img (2).png
img (3).png

I also have a list of names that I got from another folder with the same amount of files.我还有一个名称列表,这些名称是从另一个具有相同数量文件的文件夹中获得的。 Now I want to rename the images folder with new names using the names in the list.现在我想使用列表中的名称用新名称重命名图像文件夹。

import os
org = os.listdir('C:/Python310/py_scripts/new')
fake = os.listdir('C:/Python310/py_scripts/fake')





org = [i.replace('.PNG', '.png') for i in org]


for i in org:
    for s in fake:
        os.rename(f'C:/Python310/py_scripts/fake/{s}', f'C:/Python310/py_scripts/nw/{i}')

the images are in folder 'fake' and the list was from the names in folder 'new' and 'nw' is where I want the renamed images to be.图像位于“fake”文件夹中,列表来自文件夹“new”中的名称,而“nw”是我希望重命名图像的位置。

This is the output这是输出

FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:/Python310/py_scripts/fake/0001 (1).png' -> 'C:/Python310/py_scripts/nw/0008.png'

Try this:尝试这个:

import os


org = os.listdir('C:/Python310/py_scripts/new')
fake = iter(os.listdir('C:/Python310/py_scripts/fake'))

org = [i.replace('.PNG', '.png') for i in org]

### If both folders contain the same amount of files.
for i in org:
    os.rename(f'C:/Python310/py_scripts/fake/{next(fake)}', f'C:/Python310/py_scripts/nw/{i}')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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