2016-10-25 41 views
0

我正在嘗試編寫一個排序我的下載的程序;然而,當我嘗試運行它,我得到這個:製作文件排序腳本時出錯

Traceback (most recent call last): 
    File "/usr/lib/python3.5/shutil.py", line 538, in move 
    os.rename(src, real_dst) 
FileNotFoundError: [Errno 2] No such file or directory:'UntitledDocument.txt' -> 'Downloads/TxtFiles/UntitledDocument.txt' 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "/home/nicholas/Python/DownloadSorter.py", line 19, in <module> 
    shutil.move(file, folders[fileType]) 
    File "/usr/lib/python3.5/shutil.py", line 552, in move 
    copy_function(src, real_dst) 
    File "/usr/lib/python3.5/shutil.py", line 251, in copy2 
    copyfile(src, dst, follow_symlinks=follow_symlinks) 
    File "/usr/lib/python3.5/shutil.py", line 114, in copyfile 
    with open(src, 'rb') as fsrc: 
FileNotFoundError: [Errno 2] No such file or directory: 'UntitledDocument.txt' 

這是代碼: #在/ usr/bin中/ python3 進口OS 進口shutil

folders = { 
    '.tar.gz': 'Downloads/TarFiles', 
    '.deb': 'Downloads/DebFiles', 
    '.iso': 'Downloads/IsoFiles', 
    '.txt': 'Downloads/TxtFiles', 
    '.exe': 'Downloads/ExeFiles', 
    '.mp3': 'Videos', 
    '.wav': 'Music' 
} 
os.chdir('/home/nicholas/') 

for file in os.listdir('Downloads'): 
    for fileType in folders.keys(): 
     if file.endswith(fileType): 
      shutil.move(file, folders[fileType]) 

回答

0

的問題是,您正在使用沒有路徑的文件名,shutilDownloads目錄的工作目錄intead中查找該文件。

修復的方法是簡單的:

shutil.move(os.path.join('Downloads', file), folders[fileType]) 
+0

...仍然假定目標目錄確實存在。 – tripleee

+0

你說得對。但是我們一次只處理一個錯誤:) – table