简体   繁体   English

删除字符并重命名文件

[英]Removing characters and renaming files

Having trouble shortening to the first 13 characters of a file name, any suggestions? 在将文件名缩写到前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')

Keeping getting the error 不断收到错误

'FileNotFoundError: [Errno 2] No such file or directory:' 'FileNotFoundError:[错误2]没有这样的文件或目录:'

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

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

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