首页 > 解决方案 > windows上的python权限被拒绝

问题描述

大家好,我正在尝试构建一个从 xlsx 到 csv 的自动转换器,这是我转换文件的函数

def on_modified(self, event):
        print(f'event type: {event.event_type}  path : {event.src_path}')
        fileToConvert = pd.read_excel (event.src_path)
        fileToConvert.to_csv(pathOutput)

事件对象是从库派生的,它返回所需的结果,pd 是我导入熊猫的方式,但是当我运行代码时出现此错误

Traceback (most recent call last):
File "C:\Python38\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Python38\lib\site-packages\watchdog\observers\api.py", line 203, in run
self.dispatch_events(self.event_queue, self.timeout)
File "C:\Python38\lib\site-packages\watchdog\observers\api.py", line 376, in dispatch_events
handler.dispatch(event)
File "C:\Python38\lib\site-packages\watchdog\events.py", line 331, in dispatch
{
File "converter.py", line 12, in on_modified
fileToConvert.to_csv(pathOutput)
File "C:\Python38\lib\site-packages\pandas\core\generic.py", line 3170, in to_csv
formatter.save()
File "C:\Python38\lib\site-packages\pandas\io\formats\csvs.py", line 185, in save
f, handles = get_handle(
File "C:\Python38\lib\site-packages\pandas\io\common.py", line 493, in get_handle
f = open(path_or_buf, mode, encoding=encoding, errors=errors, newline="")
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\isaac\\Desktop\\converter_output' 

它基本上给了我一个权限错误,我尝试以管理员身份运行它,但遇到了同样的错误,任何帮助将不胜感激在此先感谢

标签: pythonpandaspython-watchdog

解决方案


由于您在 Window 上,权限错误的原因是您正在写入一个写保护的文件夹,并且您需要显示您的代码有权写入该文件夹。如果您在命令提示符或 Anaconda cmd 中运行代码,则在运行 cmd 之前,右键单击它并选择run as administrator. 然后运行你的程序,它会没事的。如果您在 IDE(例如 VS 代码)中运行它,请关闭它并右键单击其图标并选择run as administrator.


推荐阅读