【发布时间】:2021-03-17 07:33:33
【问题描述】:
当我运行这段代码时:
def register_json(path, _dict):
from json import dump
with open(path, 'w') as f:
dump(_dict, f)
json_path = "C:\Program Files (x86)\RT Forms\debug.json"
my_new_dict = {"yolo":"swag"}
register_json(json_path, my_new_dict)
我收到以下错误消息:
PermissionError: [Errno 13] Permission denied: 'C:\\Program Files (x86)\\RT Forms\\debug.json'
所以我尝试更改我的文件的权限:
from os import chmod
json_path = "C:\Program Files (x86)\RT Forms\debug.json"
chmod(json_path, 0o777)
现在我得到这个错误:
PermissionError: [WinError 5] Accès refusé: 'C:\\Program Files (x86)\\RT Forms\\debug.json'
在创建文件期间是否有解决方案可以授予权限? (是的,我是创建文件的人,使用 pyinstaller 和 HM NSIS 编辑整个文件夹以具有创建此文件夹的 .nsi。当您执行 .exe 时,它将创建 .json 文件)。
【问题讨论】:
-
1.您应该在 Windows 路径中使用双斜杠 '\\'。 2. 非管理员用户默认对“程序文件”文件夹具有只读权限。
-
@viilpe 1. 你是对的。 2.但是我是在这个文件夹中创建文件的人,windows并没有阻止我在运行时这样做,我不能调整权限吗?在 .json 中有存储信息的好地方吗?
-
在“程序文件”中保存数据是一种不好的做法。您最好在 AppData 或其他用户文件夹中执行此操作。
标签: python windows file operating-system chmod