首页 > 解决方案 > Python 脚本无权更改文件

问题描述

在 Windows 上运行我的 Python 加密程序时,一些文件返回一个

File "C:\Users\admin\Desktop\test.py", line 71, in encrypt_file
with open(out_filename, 'wb') as outfile:

PermissionError: [Errno 13] Permission denied:

有没有办法让 Python 程序获得许可?我已经尝试以管理员身份运行我的程序,以及来自该网站的一些特权提升片段。

这是代码:

with open(in_filename, 'rb') as infile:
        with open(out_filename, 'wb') as outfile:
            outfile.write(struct.pack('<Q', filesize))
            outfile.write(iv)

标签: pythonwindowspython-3.xpermissions

解决方案


我相信您正在尝试打开具有写入权限的文件,因此您需要使用'w'

with open(out_filename, 'w') as outfile:

您可以使用特定参数进行不同的访问,

'w' - Open to write 
'r' -Open to read 
'a' - Open to append at the end of the file.

推荐阅读