无法在Python中重命名文件

问题描述 投票:-1回答:1

我生气了。我有一个简单的cv2.imwrite()命令,它只保存遵循某个规则的图像。

这是我用来保存的代码:

cv2.imwrite(os.path.expanduser('~\\Desktop\\output9\\causale-multi{}\\superiore\\box{}.png').format(file[0], i), roi)

这是我获得的文件夹:

example

困扰我的是:我无法将文件夹中的文件重命名为简单的box0,box1,box2等。

我试过os.rename ..

path = os.path.expanduser('~\\Desktop\\output9\\causale-multi{}\\superiore\\'.format(file[0]))
    for root, dirs, files in os.walk(str(path)):
        for file in enumerate(files):
            if file.endswith(".png"):
                os.rename(file, os.path.expanduser('~\\Desktop\\output9\\causale-multi{}\\superiore\\box{}.png'.format(file[0], file[0])))

...但是给出了没有属性.endswith的元组的错误。我也用过glob但什么都没用。

我怎样才能简单地重命名这些文件?

注意文件夹causale-multi{}最后有一个数字,每次执行代码时都会改变。

谢谢

python rename batch-rename
1个回答
0
投票

enumerate(files)将遍历(0, file_0)(1, file_1),...只需删除那些不必要的enumerate,或使用类似的东西:

for i, file_i in enumerate(files):

也不要使用关键字file作为变量名...

© www.soinside.com 2019 - 2024. All rights reserved.