【发布时间】:2015-10-26 00:03:42
【问题描述】:
我想使用 python 重命名目录中的所有子文件夹。我认为这相当容易,但由于我是 python 和一般编程的新手,我假设我缺少一些关键。
这是我的文件夹结构:C:\Users\DBailey\Desktop\Here
-有两个文件夹,这个文件夹命名为:3303_InfigenSolar 和 3304_FurnaceCanyon
-在这些文件夹中有 4 个其他子文件夹,这些文件夹中有一堆文件。
我只想批量重命名 3303_InfigenSolar 和 3304_FurnaceCanyon 以便它们读取 3303_InfigenSolar_08315 和 3304_FurnaceCanyon_08315
到目前为止,这是我的代码:
now = datetime.datetime.now()
month = "0" + str(now.month)
day = now.day
year1 = str(now.year)
year2 = year1[2:]
date = str(month) + str(day) + str(year2)
newname = fn + "_" + str(date)
dir = 'C:\Users\DBailey\Desktop\Here'
folder = os.listdir(dir)
for fn in folder:
print newname
os.rename(fn, newname)
当我运行脚本时 - 只打印一个文件夹(只有两个文件夹,但会添加更多),我收到以下错误:
Traceback (most recent call last):
File "<interactive input>", line 2, in <module>
WindowsError: [Error 2] The system cannot find the file specified
【问题讨论】:
-
请注意,您的代码和迄今为止提供的答案将重命名文件,因为您不只过滤文件夹。
-
@DavidBailey,考虑删除标签batch-file,因为这实际上不适用于此处。
标签: python batch-file rename