首页 > 解决方案 > 如何获得使用 Python 在 C:\\Program Files 中创建文件的权限?

问题描述

如何访问在 Python 中的 C:\Program Files 中创建文件?我已将我的程序编译为 exe。

我的控制台显示:

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC 
v.1914 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

>>> fl = open('C:\\Program files\\example.example', 'w')

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
fl = open('C:\\Program files\\example.example', 'w')
PermissionError: [Errno 13] Permission denied: 'C:\\Program files\\example.example'

我的意思是如何在其中创建文件?或者我怎样才能获得管理员权限?没有右键单击并选择以管理员身份运行?像其他应用程序一样吗?

标签: python-3.x

解决方案


你不能从一个程序中提升你的权限,这会对抗整个权限系统。最简单和最安全的解决方案是以管理员身份运行该程序 - 当其他人运行它时让它失败。

另一种方法是从内部运行第二个进程,并允许该进程请求管理员权限,尽管这将是一个巨大的安全漏洞。


推荐阅读