2017-07-07 176 views
0

這是我的代碼:無法在python中移動文件?錯誤號13-被拒絕的權限

def unpack(folders): 
for folder in folders: 
    files = os.listdir(folder) 
    print (files) 
    while len(os.listdir(folder)) != 0: 
     for file in files: 
      if os.path.isdir(file)==False: 
       print (file) 
       shutil.move(os.path.join(cur_dir,folder,file),os.path.join(cur_dir,file)) 
      else: 
       unpack(file) 


    if len(os.listdir(folder))==0: 
     os.rmdir(folder) 

當我把這個目錄上這個節目是,一切工作正常,但我不能複製一個名爲「desktop.ini的」文件。這是錯誤:

Traceback (most recent call last): 
    File "C:\Users\satvi_000\AppData\Local\Programs\Python\Python36\lib\shutil.py", line 544, in move 
    os.rename(src, real_dst) 
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:\\Users\\satvi_000\\Downloads\\others\\desktop.ini' -> 'C:\\Users\\satvi_000\\Downloads\\desktop.ini' 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "C:\Users\satvi_000\Downloads\clean_folder.py", line 37, in <module> 
    unpack(folders_list) 
    File "C:\Users\satvi_000\Downloads\clean_folder.py", line 30, in unpack 
    shutil.move(os.path.join(cur_dir,folder,file),os.path.join(cur_dir,file)) 
    File "C:\Users\satvi_000\AppData\Local\Programs\Python\Python36\lib\shutil.py", line 558, in move 
    copy_function(src, real_dst) 
    File "C:\Users\satvi_000\AppData\Local\Programs\Python\Python36\lib\shutil.py", line 257, in copy2 
    copyfile(src, dst, follow_symlinks=follow_symlinks) 
    File "C:\Users\satvi_000\AppData\Local\Programs\Python\Python36\lib\shutil.py", line 121, in copyfile 
    with open(dst, 'wb') as fdst: 
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\satvi_000\\Downloads\\desktop.ini' 

我猜這是一個系統文件或其他東西。我如何克服這個問題?移動文件並不是完全必要的,跳過它很好。

+1

不會'嘗試:shutil.move(...)除了:pass'就夠了嗎? (如果失敗,它將忽略錯誤) – Nuageux

+1

它看起來像文件(你想移動到哪裏)已經存在。你可以刪除該文件,如果這是你想要的。 – syntonym

回答

2

您的錯誤已經包含所有需要的信息:FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:\\Users\\satvi_000\\Downloads\\others\\desktop.ini' -> 'C:\\Users\\satvi_000\\Downloads\\desktop.ini'

A desktop.ini文件是Windows上隱藏的系統文件,包含有關特殊外觀或文件夾名稱的信息。

desktop.ini文件在我Documents文件夾中的示例內容:

[.ShellClassInfo] 
[email protected]%SystemRoot%\system32\shell32.dll,-21770 
IconResource=%SystemRoot%\system32\imageres.dll,-112 
IconFile=%SystemRoot%\system32\shell32.dll 
IconIndex=-235 

你可以看到它包含了(它會自動顯示爲Dokumente在德語Windows)關於本地化的名稱信息,一個特殊的圖標有時候是「虛構文件夾」的屬性。這意味着你應該而不是嘗試移動這些文件,因爲它們可能會破壞文件夾的正確外觀和屬性(考慮回收站)。

由於在平均windows系統上有很多desktop.ini文件,遇到這種問題並不少見。在我的系統,目前有166名這樣的文件:

>>> from glob import glob 
>>> print(len(glob(r"c:\**\desktop.ini", recursive=True))) 
166 

個人而言,我會建議一樣Nuageux - 只是try移動和記錄/忽略錯誤:

try: 
    shutil.move(os.path.join(cur_dir,folder,file),os.path.join(cur_dir,file)) 
except FileExistsError as e: 
    print("The file {} already exists. Error message: {}".format(os.path.join(cur_dir,file), e)) 

另一種方法是檢查對於每個文件,如果它的名字是desktop.ini

1

看的堆棧跟蹤的第一個錯誤:

FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:\Users\satvi_000\Downloads\others\desktop.ini' -> 'C:\Users\satvi_000\Downloads\desktop.ini'

doc of os.rename (which is used by shutil.move)說這有關Windows:

On Windows, if dst already exists, OSError will be raised even if it is a file; there may be no way to implement an atomic rename when dst names an existing file

所以,你必須檢查該文件不存在移動前:

if os.path.exists(path): 
    continue 
+0

其實我認爲他得到OSError是因爲他試圖覆蓋系統文件,而Windows會冒犯到這一點。運行從提升的命令提示符更改它的腳本可能是一個答案? – BoboDarph

+0

@BoboDarph,我手邊沒有一個窗口來檢查,但我懷疑\ Downloads \文件夾將包含系統文件。 此外,該錯誤明確指出,它不能創建一個已經存在的文件。 當然,我可能是錯的,但我很確定在這種情況下。 – iCart

+0

如果OP友好地運行以下命令,您可以輕鬆找到:attrib C:\ Users \ satvi_000 \ Downloads \ others \ desktop.ini。如果他獲得HS屬性,那麼您的罪魁禍首是:https://superuser.com/questions/44812/windows-explorers-file-attribute-column-values – BoboDarph

相關問題