Writing Python engineering code took me 5 hours to troubleshoot, and finally found out that the permission was not enough when writing to the file. .
I do a test:
Write a file to the root directory of drive C:
a = "C:\\日志_test.txt"
with open(a, 'w', encoding='utf-8') as file_object:
file_object.write(a)
Error after running:
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
Then I changed the C in the source code to D:
a = "D:\\日志_test.txt"
with open(a, 'w', encoding='utf-8') as file_object:
file_object.write(a)
No more errors after running:
D:\Code\check_tool\venv\Scripts\python.exe D:/Code/check_tool/222.py
进程已结束,退出代码0
I was also drunk. . .
Therefore, if you write files in Python, you must consider the invisible problem of system permissions.