import os
path ='/Users/kunalmalhotra/Desktop/HotdogNotHotdog/hotdog/'
for filename in os.listdir(path):
if filename.endswith(".jpg"):
for filename in os.listdir(path):
for i in range(0,100):
os.rename(filename, "hotdog"+str(i)+".jpg")
答案 0 :(得分:1)
你有一些不需要的循环。只需使用计数器标志即可根据需要重命名文件。
<强>实施例强>
import os
path ='/Users/kunalmalhotra/Desktop/HotdogNotHotdog/hotdog/'
c = 0 #use flag as counter.
for filename in os.listdir(path):
if filename.endswith(".jpg"):
fileN = os.path.join(path, filename)
fileToRename = os.path.join(path, "hotdog"+str(c)+".jpg")
os.rename(fileN, fileToRename)
c += 1