当您试图在没有必要权限的情况下从Python访问文件时,会发生
使用打开文件时出现Python错误
f = open(file, "w")
但它的开场白是:
f = open(file, "r")
1-运行我的应用时关闭文件
2-应用程序以管理员身份运行
3-文件没有设置为只读,应该有写权限(使用windows(
我仍然得到这个错误:
[Errno 13] Permission denied: 'file.txt'
我无法解决这个问题。如果有人有任何想法,请告诉我。
PermissionError: [errno 13]
权限拒绝错误。
解决方案N.1
请尝试指定文件的完整路径。错误可能是,当不使用完整路径时,Python使用当前目录,这与默认目录不同。
f = open('C:my foldermyOutput.txt', 'w')
解决方案N.2
path = Path("path/to/my/file.txt")
path.parent.mkdir(parents=True, exist_ok=True)
with open(path, "w") as file:
...
解决方案N.3
import os
path = r"my/path/to/file.txt"
assert os.path.isfile(path)
with open(path, "r") as f:
pass
解决方案N.4
from os import path
file_path = path.relpath("xxx/file.txt")
with open(file_path) as f:
<do stuff>