我在一个文件夹中设置了100个文件。我需要重命名文件。我的输入文件就像
1) 0000000001.001
2) 0000000001.002
3) 0000000001.003
4) 0000000002.001
5) 0000000002.002
6) 0000000002.003
7) 0000000003.001
8) 0000000003.002
9) 0000000003.003
我已经尝试过了:
import os
folder ="C:/Users/xyz"
rename_dict = {'001': '1', '002': '2', '005': '5'}
for filename in os.listdir(folder):
base_file, ext = os.path.splitext(filename)
ext = ext.replace('.','')
if ext in rename_dict:
new_ext = rename_dict[ext]
new_file = base_file + new_ext
old_path = os.path.join(folder, filename)
new_path = os.path.join(folder, new_file)
os.rename(old_path, new_path)
例外的输出:
1) 0000000101.1
2) 0000000101.2
3) 0000000101.3
4) 0000000102.1
5) 0000000102.2
6) 0000000102.3
7) 0000000103.1
8) 0000000103.2
9) 0000000103.3
我需要以增量方式重命名文件名。
答案 0 :(得分:0)
在这里(假设它们是.txt文件)
import os
folder ="C:\\Users\\xyz\\"
os.chdir(folder)
for filename in os.listdir(folder):
newName = filename.split(".")[0] + "." + filename.split(".")[1][-1:] + '.txt'
print newName
os.rename(filename, newName)