无法在python中移动文件?错误号13-被拒绝的权限

无法在python中移动文件?错误号13-被拒绝的权限

问题描述:

这是我的代码:无法在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

您的错误已经包含所有需要的信息: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

看的堆栈跟踪的第一个错误:

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