无法在python中移动文件? Errno 13-许可被拒绝

时间:2017-07-07 09:27:23

标签: python file copying

这是我的代码:

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'

我猜这是系统文件或其他东西。我如何通过这个?移动文件并不是完全必要的,跳过它很好。

2 个答案:

答案 0 :(得分:2)

查看stacktrace中的第一个错误:

  

FileExistsError:[WinError 183]当该文件已存在时无法创建文件:&#39; C:\ Users \ satvi_000 \ Downloads \ others \ desktop.ini&#39; - &GT; &#39; C:\ Users \用户satvi_000 \下载\的desktop.ini&#39;

doc of os.rename (which is used by shutil.move)说这个关于windows:

  

在Windows上,如果dst已经存在,即使它是文件,也会引发OSError;当dst命名现有文件时,可能无法实现原子重命名

因此,您必须在移动前检查文件是否存在:

if os.path.exists(path):
    continue

答案 1 :(得分: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'

desktop.ini文件是Windows上的隐藏系统文件,包含有关文件夹特殊外观或名称的信息。

desktop.ini文件夹中Documents文件的示例内容:

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

您可以看到它包含有关本地化名称的信息(在德语版本上,它会自动显示为Dokumente),一个特殊的图标,有时也包含“虚拟文件夹”的属性。这意味着您应该尝试移动这些文件,因为它们可能会破坏文件夹的正确外观和属性(想想回收站)。

由于平均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