[英]Rename All Files in a Directory with consecutive numbers in Python
I would like to rename all file (the filenames have no clear pattern) into filenames with consecutive numbers eg: 我想将所有文件(文件名没有清晰的模式)重命名为带有连续数字的文件名,例如:
Files in Directory: 目录中的文件:
agh_uio78.jpg hhaq23klp.png mickey.tiff agh_uio78.jpg hhaq23klp.png mickey.tiff
into 成
001.jpg 002.png 003.tiff 001.jpg 002.png 003.tiff
This will give me a result with no extension: 这将给我一个没有扩展的结果:
import os
FList = os.listdir(os.getcwd())
FListC = FList[1:]
m = 0
for i in FListC:
os.rename(i,str(m))
m = m+1
Result: 结果:
1 2 3 1 2 3
import os
FList = os.listdir(os.getcwd())
FListC = FList[1:]
m = 0
for i in FListC:
fileExtension = os.path.splitext(i)[1]
os.rename(i,str(m)+fileExtension)
m = m+1
You forgot to save the extension of the file. 您忘记保存文件的扩展名。
Above code will grab the file's extension and concatenate to your incremented file name 上面的代码将获取文件的扩展名并连接到您增加的文件名
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.