How to set path in Windows NT?

David LeBlanc whisper at oz.nospamnet
Wed May 16 13:50:43 EDT 2001


In article <j6j4gtost1j98cqlg5td32ndh176fsktt5 at 4ax.com>, 
Wolfgang.Strobl at gmd.de says...
> kc5tja at dolphin.openprojects.net (Samuel A. Falvo II) schrieb am Tue, 15
> May 2001 23:55:26 GMT:
> 
> >Not strictly Python related, but...
> >
> >I've written a utility batch file that I'd like to be executable after
> >installing it on a computer.  How can I programmatically set the PATH
> >environment variable to include a directory of my choice, such that the next
> >time the user logs in or runs the command prompt, the variable will be set
> >accordingly?
> 
> Read and try the following script, and don't forget to repair your path
> afterwards. :-)
> 
> import _winreg
> x=_winreg.ConnectRegistry(None,_winreg.HKEY_LOCAL_MACHINE)
> y=_winreg.OpenKey(x,"SYSTEM\CurrentControlSet\Control\Session
> Manager\Environment")
> print "Your environment variables are"
> print "#","name","value","type"
> for i in range(1000):
>     try:
>         n,v,t=_winreg.EnumValue(y,i)
>         print i,n,v,t
>     except EnvironmentError:
>         print "You have",i,"Environment variables"
>         break
> print "Your PATH was "    
> path = _winreg.QueryValueEx(y,"path")[0]
> print path
> _winreg.CloseKey(y)
> # Reopen Environemt key a second time,  for writing.
> y=_winreg.OpenKey(x,"SYSTEM\CurrentControlSet\Control\Session
> Manager\Environment",0,_winreg.KEY_ALL_ACCESS)
> # now append C:\ to the path
> _winreg.SetValueEx(y,"path",0,_winreg.REG_EXPAND_SZ,path+";C:\\")
> _winreg.CloseKey(y)
> _winreg.CloseKey(x)
> 
> 
saved this as winenv.py. running it got:
[k:/Python21/Projects]% python winenv.py
Traceback (most recent call last):
  File "winenv.py", line 3, in ?
    y=_winreg.OpenKey(x,"SYSTEM\CurrentControlSet\Control\SessionManager\
Environment")
WindowsError: [Errno 2] The system cannot find the file specified

What's wrong?

Thanks,

Dave LeBlanc



More information about the Python-list mailing list