首页 > 解决方案 > 使用 shutil.move 时出现 Python 3.8 权限错误

问题描述

我正在尝试编写一个 python 代码,它将监视特定文件夹中的任何“修改”事件,然后将行从源 csv 文件复制到目标 csv 文件。我在 Windows 上使用 Python 3.8

复制行后,我想将源文件移动到存档文件夹,这就是我收到“Win32 权限错误”的时候

有人能帮忙吗?

类处理程序(文件系统事件处理程序):

@staticmethod
def on_any_event(event):
    if event.is_directory:
        return None

    elif event.event_type == 'modified':
        # Take any action here when a file is place in the Watch Folder.
        nsrc_file = event.src_path
        dst_file = "C:\\Users\\paramm\\Documents\\Python\\WB_output\\OE_2019.csv"
        archive_folder = "C:\\Users\\paramm\\Documents\\Python\\WB_archive"

        with open(nsrc_file, "r") as f_input, open(dst_file, "a", newline='') as f_output:
            csv_input = csv.reader(f_input)

            for row in csv_input:
                csv.writer(f_output).writerows(csv_input)

            shutil.move(nsrc_file, archive_folder)

错误:PermissionError:[WinError 32] 该进程无法访问该文件,因为它正被另一个进程使用:

标签: python

解决方案


推荐阅读