我有许多图像文件存储在一个文件夹中,分别是0.png,1.png,…,x.png。我必须按相反的顺序重命名,即0->;x,1->;(x-1),…(x-1)->;1,x->;0。我用python编写了以下代码。在
for filename in os.listdir("."):
tempname = "t" + filename
os.rename(filename, tempname)
for x in range(minx, maxx+1):
tempname = "t" + str(x) + ".png"
newname = str(maxx-x) + ".png"
os.rename(tempname, newname)
我遇到了以下错误:
^{pr2}$我做错什么了? 有没有更聪明的方法?在
尝试下面的方法,它使用
glob
模块来获取文件列表。这应该包括完整路径,否则os.rename
可能会失败:注意,如果您只想以
.png
文件为目标,可以将glob行改为*.png
相关问题 更多 >
编程相关推荐