删除字符和重命名文件

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

难以缩短到文件名的前13个字符,有什么建议吗?

import os

path = '/home/ben/Desktop/UK_DDV'
files = os.listdir(path)

for file in files:
    outfile = file[:13]
    os.rename(os.path.join(path, outfile), os.path.join(path, outfile)+'.tif')

保持错误

'FileNotFoundError:[Errno 2]没有这样的文件或目录:'

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

os.rename()的第一个参数应该使用原始文件名file而不是outfile

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