search

Home  >  Q&A  >  body text

windows - python 文件读写 权限 permission denied

我写了一个python小工具,安装在C:\Program Files (x86)\appname目录下,可是无法创建和读写文件,出现permission denied错误提示。
之前程序是可以在C:\Users\XXX\AppData\Local\VirtualStore\Program Files (x86)创建文件的,因为改了一大堆东西,而且代码也很长,就不贴代码了。
如何做才能在C:\Users\XXX\AppData\Local\VirtualStore\Program Files (x86)\appname创建文件,不关闭UAC,不使用管理员权限。

就像这样的代码:

# -*- coding: utf-8 -*-
with open("a.txt","w")as f:
    pass

操作系统:windows 10 专业版 1607 开启UAC

python 2.7.11

伊谢尔伦伊谢尔伦2801 days ago3852

reply all(3)I'll reply

  • 巴扎黑

    巴扎黑2017-04-18 09:41:05

    It has something to do with the directory. This directory under Windows requires administrator rights. You can go to other directories such as the E:AC directory to operate files

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 09:41:05

    Please add it.
    I hope to use UAC virtualization, but this is a practice that Windows does not advocate. The question asks about the reasons and solutions for UAC virtualization failure. I was able to virtualize successfully before, but I don't know why it failed.
    Also gives the recommended method for windows. Create files in other directories. Use os.getenv('ALLUSERSPROFILE')或者os.environ.get('ALLUSERSPROFILE')获取系统环境变量,然后构造路径。相关的变量还有LOCALAPPDATAAPPDATA to correspond to different directories. Perhaps with a suitable method, this problem will disappear. It is better to obtain system variables and write them into files instead of creating folders on the C drive or other drives. What if there is no other DEF drive letter, or the system drive letter is not the C drive? The program should not be written to death. It is best to follow Microsoft's suggestions and everyone's agreed practices. Finally, I lament that segmentfault is not active enough. The questions I asked are basically self-asked and answered. Let me complain again, the text editor is not easy to use.

    UAC related links

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:41:05

    You can divide directory permissions. If you must create files in this directory, give this folder writable permissions. Inheriting permissions depends on your needs

    reply
    0
  • Cancelreply