[英]how to rename files in multiple folders numerically starting with '1' each folder using python?
i'm trying to rename files numerically in multiple folders but it seems that the count continues to run through all the files and not starting from '1' each folder. 我正在尝试以数字方式重命名多个文件夹中的文件,但似乎计数继续遍历所有文件,而不是从每个文件夹的“ 1”开始。 what should i do?
我该怎么办?
i have tried the following code: 我尝试了以下代码:
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
This should work: 这应该工作:
for folderName, subfolders, filenames in os.walk(path):
count = 1 # << -- here are the changes
for f in filenames:
f_name, f_ext = os.path.splitext(f)
f_name = str(count)
count += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.