写Python工程代码,花了我5个小时排错,最后发现是写入文件的时候,权限不够。。
我做个测试:
向C盘根目录写入文件:
a = "C:\\日志_test.txt"
with open(a, 'w', encoding='utf-8') as file_object:
file_object.write(a)
运行后报错:
D:\Code\check_tool\venv\Scripts\python.exe D:/Code/check_tool/222.py
Traceback (most recent call last):
File "D:/Code/check_tool/222.py", line 2, in <module>
with open(a, 'w', encoding='utf-8') as file_object:
PermissionError: [Errno 13] Permission denied: 'C:\\日志_test.txt'
进程已结束,退出代码1
然后我把源代码中的C变成D:
a = "D:\\日志_test.txt"
with open(a, 'w', encoding='utf-8') as file_object:
file_object.write(a)
运行后不再报错:
D:\Code\check_tool\venv\Scripts\python.exe D:/Code/check_tool/222.py
进程已结束,退出代码0
我也是醉了。。。
所以在Python中写入文件的话,一定得考虑好系统权限这个隐形问题。