有时候大家用Python打开文件时发现会有错误消息PermissionError: [Errno 13] Permission denied:之所以会出现这种问题是因为文件被系统加密了,没有管理员权限就打不开,即使你是管理员,也没法用Python打开。
比如说C:\Program Files中有文件000.txt和程序000.py
#000.py
import time
with open("000.txt","r")as file:
print(file.read())
file.close()
with open("000.txt","w+")as file:
file.write("abcabcabcabc")
file.close()
#000.txt
123123123123
当运行该程序时,会显示
123123123123
Traceback (most recent call last):
File "C:\Program Files\000.py", line 5, in
with open("000.txt","w+")as file:
PermissionError: [Errno 13] Permission denied: '000.txt'
右击000.txt,单击属性,然后跟着图片做吧
再次运行代码,试一试效果:
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
======================= RESTART: C:\Program Files\000.py =======================
123123123123
>>>
可以看到实验成功了。如果还不行,就在权限页面再添加上ALL APPLICATION PACKAGES。