另一个进程正在使用Python PermissionError:

问题描述

我正在尝试将移动文件的2版本从2:53开始,版本1从1:18 https://www.youtube.com/watch?v=qbW6FRbaSl0

开始

我有代码,并且如果我按住CTRL + lmouse单击/将文件拖到“ test1”文件夹中,它将对其重命名并将其传输到“ test2”文件夹中。

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
#pip install watchdog

import os
import time


class MyHandler(FileSystemEventHandler):
    i = 1

    def on_modified(self,event):
        new_name = "new_file_" + str(self.i) + ".txt"
        for filename in os.listdir(folder_to_track):
            file_exists = os.path.isfile(folder_destination + "/" + new_name)
            while file_exists:
                self.i += 1
                new_name = "new_file_" + str(self.i) + ".txt"
                file_exists = os.path.isfile(folder_destination + "/" + new_name)

            src = folder_to_track + "/" + filename
            new_destination = folder_destination + "/" + new_name
            os.rename(src,new_destination)


folder_to_track = '/Users/TH/Desktop/test1'
folder_destination = '/Users/TH/Desktop/test2'
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler,folder_to_track,recursive=True)
observer.start()

try:
    while True:
        time.sleep(10)
except KeyboardInterrupt:
    observer.stop()
observer.join()

我的问题是,如果我尝试将另一个文件2)拖到“ Test1”文件夹中,那么我会收到此错误...(然后,我需要重新启动pc,然后可以对第一个文件进行操作,然后第二个文件将再次开始错误。)

PermissionError:[WinError 32]进程无法访问文件 因为它正在被另一个进程使用: '/Users/TH/Desktop/test1/skole.txt'-> '/Users/TH/Desktop/test2/new_file_2.txt'

有人可以帮助/指导我吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)