简体   繁体   English

如何使用 Python 将任何目录添加到 PATH?

[英]How Can I add any directory to PATH using Python?

I'm working on a project which adds a certain directory to the Windows Path Environment variable.我正在开发一个将某个目录添加到 Windows 路径环境变量的项目。 But I can't figure out how to add a directory to Path using Python但我不知道如何使用 Python 将目录添加到路径

You will need to make sure to run this as an account with Admin privileges您需要确保以具有管理员权限的帐户运行它

import win32com.shell.shell as shell

def addPathToEnv(pathToAdd):
    """
    Add the supplied path the Windows Path Environment variable
    :param pathToAdd: <str> Full path to be added as Path Environment.
    """
    
    commands = f'setx /M PATH "%PATH%;{pathToAdd}"'
    shell.ShellExecuteEx(lpVerb='runas', lpFile='cmd.exe', lpParameters='/c '+commands)

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

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