我可以通过使用“以管理员身份运行”打开命令提示符来运行脚本。
如果您以提升的权限运行脚本,您似乎只能维护 HKEY_LOCAL_MACHINE 条目。
根据此 MSDN 链接,一些 HKEY_CLASSES_ROOT 条目来自 HKEY_LOCAL_MACHINE :
HKEY_CLASSES_ROOT 子树是由 HKEY_CURRENT_USER\Software\Classes 和 HKEY_LOCAL_MACHINE\Software\Classes 合并而成的视图
我更新了脚本以包含建议的 try/except 以及一些打印语句以获得额外反馈。
这是我更新脚本的方式:
""" Change the .py file extension to point to a different
Python installation.
"""
import _winreg as reg
import sys
pydir = sys.argv[1]
todo = [
('Applications\python.exe\shell\open\command',
'"PYDIR\\python.exe" "%1" %*'),
('Applications\pythonw.exe\shell\open\command',
'"PYDIR\\pythonw.exe" "%1" %*'),
('Python.CompiledFile\DefaultIcon',
'PYDIR\\pyc.ico'),
('Python.CompiledFile\shell\open\command',
'"PYDIR\\python.exe" "%1" %*'),
('Python.File\DefaultIcon',
'PYDIR\\py.ico'),
('Python.File\shell\open\command',
'"PYDIR\\python.exe" "%1" %*'),
('Python.NoConFile\DefaultIcon',
'PYDIR\\py.ico'),
('Python.NoConFile\shell\open\command',
'"PYDIR\\pythonw.exe" "%1" %*'),
]
classes_root = reg.OpenKey(reg.HKEY_CLASSES_ROOT, "")
for path, value in todo:
print "Updating %s with %s" % (path, value.replace('PYDIR', pydir))
try:
key = reg.OpenKey(classes_root, path, 0, reg.KEY_SET_VALUE)
reg.SetValue(key, '', reg.REG_SZ, value.replace('PYDIR', pydir))
except:
print "Unable to maintain %s\n" % (path)