Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python';s在Windows中更改ntp服务器设置的子进程_Python_Windows_Subprocess_Ntp - Fatal编程技术网

使用Python';s在Windows中更改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\"']

因此,我尝试使用以下方法更改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\"'])
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