简体   繁体   English

如何使用 python 添加 windows 系统路径?

[英]how to add windows system path with python?

I want to add folder to the windows path with python.我想使用 python 将文件夹添加到 windows 路径。 I tried add folder with this codes.我尝试使用此代码添加文件夹。

path = os.path.dirname(os.path.abspath(__file__))
os.system('setx /M path "%path%;'+path+'"')

this code output is =>此代码 output 是 =>

ERROR: Access to the registry path is denied.错误:拒绝访问注册表路径。

and I tried this code我试过这段代码

os.environ['PATH'] += path

When I print this code, it prints the environment variables on the screen, but when I open the environment variables page, this value does not appear.当我打印这段代码时,它会在屏幕上打印环境变量,但是当我打开环境变量页面时,这个值不会出现。 How do I add this file path?如何添加此文件路径?

thanks.谢谢。

As you can see the error which is:如您所见,错误是:

ERROR: Access to the registry path is denied.

So, my suggestion would be running your program with the elevated permission.因此,我的建议是使用提升的权限运行您的程序。 You can do so my using is 'admin' module你可以这样做我使用的是'admin'模块

import admin
if not admin.isUserAdmin():
    admin.runAsAdmin()

And try running your code as admin user并尝试以管理员用户身份运行您的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM