使用Python';s在Windows中更改ntp服务器设置的子进程
因此,我尝试使用以下方法更改Windows(XP和7)中的ntp服务器设置:使用Python';s在Windows中更改ntp服务器设置的子进程,python,windows,subprocess,ntp,Python,Windows,Subprocess,Ntp,因此,我尝试使用以下方法更改Windows(XP和7)中的ntp服务器设置: import subprocess subprocess.call(['net', 'stop', 'w32time']) subprocess.call(['reg', 'add','HKLM\Software\Microsoft\Windows\CurrentVersion\DateTime\Servers', '/f /v \"0\" /t REG_SZ /d \"ntp.craven.k12.nc.us\"']
import subprocess
subprocess.call(['net', 'stop', 'w32time'])
subprocess.call(['reg', 'add','HKLM\Software\Microsoft\Windows\CurrentVersion\DateTime\Servers', '/f /v \"0\" /t REG_SZ /d \"ntp.craven.k12.nc.us\"'])
subprocess.call(['reg', 'add', 'HKLM\Software\Microsoft\Windows\CurrentVersion\DateTime\Servers', '/f /v \"(Default)\" /t REG_SZ /d \"0\"'])
subprocess.call(['net', 'start', 'w32time'])
subprocess.call(['w32tm', '/resync'])
但这失败得很惨。我确信问题在于如何设置参数的格式,但我还没有找到正确的方法。您最后的参数没有拆分。您可能需要将
'/f/v\'0\'/t REG_SZ…'
替换为]+['/f','/v',0','/t',REG_SZ']+[…]
或者,将整个命令作为字符串传递(就像在命令行上一样)。是的,前两个和后两个命令也会按照我的预期运行,编辑注册表的命令被证明是棘手的。无关:编辑注册表不需要
子进程
,请注意,您需要转义“\”字符或使用原始字符串,例如,'HKLM\\Software\\Microsoft\\…'
或r'HKLM\Software\Microsoft\…'
@J.F.Sebastian我得出结论,在版本3中包含winreg是有充分理由的,但我仅限于使用2.7。我注意到,如果使用reg查询执行subprocess.call,它将给出与直接运行它不同的响应。知道为什么吗?winreg在Python 2中被称为_winreg