我有一些图像文件存储为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)
我遇到以下错误:
OSError: [Errno 2] No such file or directory
我做错了什么? 有更聪明的方法吗?
答案 0 :(得分:2)
尝试以下操作,它使用List<Thread> threads = new List<Thread>();
for (int i = 0; i < 100; i++)
{
Thread t = new Thread(new ThreadStart(() =>
{
BitmapSource temp = BitmapSource.Create(1, 1, 96, 96, PixelFormats.Bgr24, null, new byte[3], 3);
temp.Freeze();
}));
t.Start();
threads.Add(t);
}
foreach (var thread in threads)
{
thread.Join();
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
模块获取文件列表。这应包括完整路径,否则glob
可能会失败:
os.rename
注意,如果您只想定位import glob
import os
source_files = glob.glob(r'myfolder\mytestdir\*')
temp_files = ['{}.temp'.format(file) for file in source_files]
target_files = source_files[::-1]
for source, temp in zip(source_files, temp_files):
os.rename(source, temp)
for temp, target in zip(temp_files, target_files):
os.rename(temp, target)
个文件,可以将glob行更改为.png