【发布时间】:2017-01-27 01:31:04
【问题描述】:
您好,我有许多不同的文件需要重命名为其他文件。我已经做到了这一点,但我想拥有它,以便我可以替换许多项目及其相应的替换项,而不是逐个键入,运行代码然后再次重新键入。
更新* 另外我需要重命名以仅更改文件的一部分而不是全部内容,因此如果有“Cat5e_1mBend1bottom50m2mBend2top-Aqeoiu31”,它只会将其更改为“'Cat5e50m1mBED_50m2mBE2U-Aqeoiu31”
import os, glob
#searches for roots, directory and files
for root,dirs, files in os.walk(r"H:\My Documents\CrossTalk\\"):
for f in files:
if f == "Cat5e_1mBend1bottom50m2mBend2top":#string you want to rename
try:
os.rename('Cat5e_1mBend1bottom50m2mBend2top', 'Cat5e50m1mBED_50m2mBE2U'))
except FileNotFoundError, e:
print(str(e))
【问题讨论】:
-
您将制作的文件名中的常见替换是什么?