无法移动 python 中的文件? Errno 13- 权限被拒绝

Can't move a file in python? Errno 13- Permission Denied

这是我的代码:

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'

我猜这是系统文件之类的。我该如何克服这个问题?不一定非要移动文件,跳过就好了。

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

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

您的错误已包含所有需要的信息: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 上的隐藏系统文件,包含有关特殊外观或文件夹名称的信息。

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

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

您可以看到它包含有关本地化名称(在德语 Windows 上自动显示为 Dokumente)、特殊图标以及 "virutal folders" 有时属性的信息。这意味着您 不应 尝试移动这些文件,因为它们可能会破坏文件夹的正常外观和属性(想想回收站)。

由于平均 windows 系统上有 很多 desktop.ini 文件,因此 运行 进入这种类型的情况并不少见问题。在我的系统上,目前有 166 个这样的文件:

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

我个人建议与 Nuageux 相同 - 只是 try 移动和 log/ignore 错误:

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