【问题标题】:PermissionError: [Errno 13] Permission denied error while loading the file with pandasPermissionError: [Errno 13] 使用熊猫加载文件时权限被拒绝错误
【发布时间】:2021-05-11 16:08:50
【问题描述】:

我收到此错误:

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 事件或 sn-p 中有什么问题吗 规格:Python 3.7.6 x86 Windows 10 x64 谢谢

【问题讨论】:

  • 更改ProfileData.csv文件的权限,让用户可以阅读。
  • 已授予读写权限,但仍然出现相同的错误
  • 计算机不会说谎。如果获得许可,您将不会收到错误消息。
  • 您确定脚本是以具有权限的用户身份运行的吗?
  • 在D:\Railway_Scenario上也检查文件夹权限

标签: python pandas watchdog python-watchdog


【解决方案1】:

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

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)

【讨论】:

    猜你喜欢
    • 2021-05-27
    • 2015-07-17
    • 2016-07-25
    • 2018-11-18
    • 2020-02-07
    • 1970-01-01
    • 2020-07-01
    • 2016-11-12
    • 2019-11-09
    相关资源
    最近更新 更多