I wrote script that checks dirs from Path and remove inaccessible dirs. Also i used snippet to run my script as admin. But when i check my Path after script execution - it's all the same.
import os
import sys
import win32com.shell.shell as shell
if __name__ == "__main__":
if os.name != 'nt':
raise RuntimeError("This script is implemented only for Windows")
ASADMIN = 'asadmin'
if sys.argv[-1] != ASADMIN:
script = os.path.abspath(sys.argv[0])
params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)
print("I am root now")
paths = os.environ.get('Path').split(';')
accessible_paths = []
for path in paths:
if os.access(path, os.R_OK):
accessible_paths.append(path)
new_path = ';'.join(accessible_paths)
os.environ['Path'] = new_path
print(new_path)
print(new_path == os.environ['Path'])
So how can i actually change environment variable by Python script ?