0

coding enviroment: windows10 python3

Here is my code:

import os
PATH_add = os.popen(
    r'''For /F "EOL=H Tokens=2,*" %G In ('%SystemRoot%\System32\reg.exe Query "HKLM\SYSTEM\CurrentControlset\Control\Session Manager\Environment" /V Path 2^>NUL') Do @%SystemRoot%\System32\setx.exe Path "%HC:\Test;" /M''')
result = PATH_add.read()
print(result)

But it show an error Error: Access to registry path is denied.

I know i need to run it as adminisator, but how can i did it.i don't mean like running the pyfile as Administaor, i just want to edit the code to fix the problem.

Danhui Xu
  • 69
  • 10
  • 2
    When I gave you [that code](https://stackoverflow.com/a/73439789), you did not mention that you were using python and wanted to add to the permanent System %Path% environment. There should be much more efficient ways of doing it via python without launching cmd.exe, reg.exe and setx.exe. Surely you haven't posted code here just to ask how it can be run elevated. How are you currently running your code? Please fully explain the process. – Compo Aug 22 '22 at 11:29
  • Perhaps [this answer](https://stackoverflow.com/a/21452408) will offer an alternative method, using just Python. – Compo Aug 22 '22 at 14:15

0 Answers0