首页 > 解决方案 > PermissionError: [Errno 13] 使用熊猫加载文件时权限被拒绝错误

问题描述

我收到此错误:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\40005139\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 926, in _bootstrap_inner
    self.run()
  File "C:\Users\40005139\AppData\Local\Programs\Python\Python37-32\lib\site-packages\watchdog\observers\api.py", line 199, in run      
    self.dispatch_events(self.event_queue, self.timeout)
  File "C:\Users\40005139\AppData\Local\Programs\Python\Python37-32\lib\site-packages\watchdog\observers\api.py", line 372, in dispatch_events
    handler.dispatch(event)
  File "C:\Users\40005139\AppData\Local\Programs\Python\Python37-32\lib\site-packages\watchdog\events.py", line 403, in dispatch        
    super().dispatch(event)
  File "C:\Users\40005139\AppData\Local\Programs\Python\Python37-32\lib\site-packages\watchdog\events.py", line 278, in dispatch        
    }[event.event_type](event)
  File "d:/Railway_Scenario/DataUpload.py", line 16, in on_created
    pd.read_csv(file)
  File "C:\Users\40005139\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\parsers.py", line 610, in read_csv      
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\40005139\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\parsers.py", line 462, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "C:\Users\40005139\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\parsers.py", line 819, in __init__      
    self._engine = self._make_engine(self.engine)
  File "C:\Users\40005139\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\parsers.py", line 1050, in _make_engine 
    return mapping[engine](self.f, **self.options)  # type: ignore[call-arg]
  File "C:\Users\40005139\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\parsers.py", line 1867, in __init__     
    self._open_handles(src, kwds)
  File "C:\Users\40005139\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\parsers.py", line 1368, in _open_handles
    storage_options=kwds.get("storage_options", None),
  File "C:\Users\40005139\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\common.py", line 647, in get_handle     
    newline="",
PermissionError: [Errno 13] Permission denied: 'D:\\Railway_Scenario\\ProfileData.csv'

运行时:

class Handler(watchdog.events.PatternMatchingEventHandler):
    def __init__(self):
        # Set the patterns for PatternMatchingEventHandler
        watchdog.events.PatternMatchingEventHandler.__init__(self, patterns=['*.xls'],
                                                             ignore_directories=True, case_sensitive=False)
      
        def on_created(self, event):
            print("Watchdog received created event - % s." % event.src_path)
            df=pd.read_excel(event.src_path)

有人可以告诉我如何读取文件 on_created 事件或片段规格中有什么问题:Python 3.7.6 x86 Windows 10 x64 谢谢

标签: pythonpandaswatchdogpython-watchdog

解决方案


这里的问题是您正在尝试打开一个尚未完全保存/创建的文件。

on_created 事件在文件/目录创建开始时立即触发,而不是在文件完全创建时触发。

因此,您应该等待文件创建完成,然后再进行读取部分。

做一个简单的检查以反复比较文件大小和延迟。完全创建文件后,文件大小保持不变,您可以继续读取文件。

我从下面的链接中找到了这个信息。它还提到了其他方式。https://www.py4u.net/discuss/187495

def wait_till_file_is_created(self, source_path):
    historicalSize = -1
    while (historicalSize != os.path.getsize(source_path)):
        historicalSize = os.path.getsize(source_path)
        time.sleep(1) # Wait for one second

def on_created(self, event):
    print("Watchdog received created event - % s." % event.src_path)
    self.wait_till_file_is_created(event.src_path)                  
    df=pd.read_excel(event.src_path)

推荐阅读