【发布时间】:2019-09-11 10:49:17
【问题描述】:
我正在尝试以数字方式重命名多个文件夹中的文件,但计数似乎继续遍历所有文件,而不是从每个文件夹的“1”开始。我该怎么办?
我尝试了以下代码:
for folderName, subfolders, filenames in os.walk(path):
for f in filenames:
f_name, f_ext = os.path.splitext(f)
f_name = str(count)
count = count + 1
new_name = '{}{}'.format(f_name, f_ext)
os.rename(os.path.join(folderName,f),os.path.join(folderName,new_name))
i expect the output renaming of the files to be:
files in folder 1: 1.tif, 2.tif, 3.tif, 4.tif
files in folder 2: 1.tif, 2.tif, 3.tif
files in folder 3: 1.tif, 2.tif, 3.tif, 4.tif
but the actual outcome is:
files in folder 1: 1.tif, 2.tif, 3.tif, 4.tif
files in folder 2: 5.tif, 6.tif, 7.tif
files in folder 3: 8.tif, 9.tif, 10.tif, 11.tif
【问题讨论】:
标签: python count rename subdirectory numeric