我試圖寫這個小批量重命名文件擴展名的腳本。我傳遞了三個參數,即文件所在的目錄,當前擴展和新擴展。與參數有問題
我得到的錯誤是
python batch_file_rename_2.py c:\craig .txt .html
Traceback (most recent call last):
File "batch_file_rename_2.py", line 13, in <module>
os.rename(filename, newfile)
WindowsError: [Error 2] The system cannot find the file specified
的代碼僅爲
import os
import sys
work_dir=sys.argv[1]
old_ext=sys.argv[2]
new_ext=sys.argv[3]
files = os.listdir(work_dir)
for filename in files:
file_ext = os.path.splitext(filename)[1]
if old_ext == file_ext:
newfile = filename.replace(old_ext, new_ext)
os.rename(filename, newfile)
[IOError當試圖打開現有文件]可能的重複(http://stackoverflow.com/questions/10802418/ioerror-when-trying-to-open-existing-files) – 2012-08-06 09:55:08
也見[python os.rename (...)將無法使用!](http://stackoverflow.com/q/4147438) – 2012-08-06 10:26:48