为什么我会得到这个错误?
我正在尝试编写一个脚本,该脚本将以目录为参数,然后遍历所述目录并重命名具有给定扩展名的文件(在本例中为.jpg)
以下是我目前掌握的情况:
选择 | 换行 | 行号
- import os
- fileDir=os.listdir("c:\\Users\\Nathan\\Desktop\\Cell Pics\\")
- count = 1
- for fn in fileDir:
- if fn[-4:] == '.jpg':
- os.rename(fn,'%03i.jpg' % count)
- count += 1
现在,当我用'.'替换上面的目录时并将脚本放到我想要影响的目录中,它会完美地完成它的工作。但是,当我将目录放入os.listdir的参数中并尝试运行它时,我得到以下错误:
选择 | 换行 | 行号
- Traceback (most recent call last):
- File "C:/Python27/test2.py", line 7, in <module>
- os.rename(fn,'%03i' % count)
- WindowsError: [Error 2] The system cannot find the file specified
我做错了什么?
我使用的是WINDOWS ATM,但这个脚本是为Unix编写的。(如果问题,让我知道)我是相当新手的脚本和Python(第一周编程),所以请说明性,如果可能的话!
提前谢谢!