简体   繁体   中英

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. But I can't figure out how to add a directory to Path using 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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