1. 程式人生 > >python添加Windows環境變量

python添加Windows環境變量

cat control 目前 powers shel windows nbsp brush 原因

1.cmd中添加方式

SET PATH=%PATH%;c:\Program Files (x86)\Wireshark

註:如上代碼添加c:\Program Files (x86)\Wireshark至Windows環境變量中

但在python中使用os.system()函數卻無法執行該命令,目前還未找到原因。

2.python操作Windows註冊表進行更改

import _winreg as wg
key_test = wg.OpenKey(wg.HKEY_LOCAL_MACHINE,r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment",0,wg.KEY_ALL_ACCESS)
path_str = wg.QueryValueEx(key_test,‘path‘)
path_str_new = path_str[0] + ‘;‘ + ‘c:\Program Files (x86)\Wireshark‘
wg.SetValueEx(key_test,‘path‘,‘‘,path_str[1],path_str_new)
wg.FlushKey(key_test)
wg.CloseKey(key_test)

註:path_str的值為(u‘C:\\Python27\\;C:\\Python27\\Scripts;C:\\Program Files (x86)\\Intel\\iCLS Client\\;C:\\Program Files\\Intel\\iCLS Client\\;%SystemRoot%\\system32;%SystemRoot%;%SystemRoot%\\System32\\Wbem;%systemroot%\\System32\\WindowsPowerShell\\v1.0\\;%systemroot%\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT;;"C:\\Program Files (x86)\\Spirent Communications\\Spirent TestCenter 4.68\\Layer 4-7 Application";C:\\Program Files\\TortoiseSVN\\bin;C:\\Python27;c:\\Program Files (x86)\\Wireshark‘, 2)

 

python添加Windows環境變量