2009-12-04 64 views
4

我試圖執行Ned Batchelder的this script來切換.py我在Windows上的兩個Python安裝之間的文件關聯。此Python腳本使用_winreg模塊(Python 3.x中的winreg)編輯某些註冊表值(修改後的路徑和值對可在腳本的todo列表中看到)。在Windows 7上使用_winreg編輯註冊表值時出現Python:WindowsError

我如下執行這個腳本:

> SwitchPy.py "C:\Program Files\Python26" 

我收到以下錯誤:

Traceback (most recent call last): 
    File "C:\Users\SuperUser\SwitchPy.py", line 30, in <module> 
    key = reg.OpenKey(classes_root, path, 0, reg.KEY_SET_VALUE) 
WindowsError: [Error 5] Access is denied 

我猜測,這可能是是與帳戶權限。但請注意:

  • 上面使用的帳戶是Administrators組的一部分,具有管理員權限。

  • 通過上述帳戶,我可以執行regedit.exe並手動設置腳本中列出的值,而不會面臨任何權限或訪問問題。

我使用Windows 7並且是域的一部分。這些問題中的任何一個都可以解決這個問題嗎?

有沒有人對此錯誤有任何線索?我如何讓這個腳本運行?

回答

0

當我嘗試那個,我得到了「路徑未找到」的錯誤Python.CompiledFile

我檢查了我的註冊表,它不存在,但不是Windows 7。

所以,我刪除的Python.CompiledFile及其運行這些線路在這裏很好,或

你可以把try: except:在打開項和的SetValue,不是好主意,但。

+0

其實,即使是我的註冊表沒有在待辦事項列表第2項。我刪除它們並運行該腳本並面對上述「訪問被拒絕」錯誤。 – 2009-12-04 09:07:20

0

我能夠通過使用「以管理員身份運行」打開命令提示符來運行該腳本。

看來,如果您使用提升權限運行腳本,則只能維護HKEY_LOCAL_MACHINE條目。

一些HKEY_CLASSES_ROOT項根據this MSDN link來自HKEY_LOCAL_MACHINE:

The HKEY_CLASSES_ROOT subtree is a view formed by merging HKEY_CURRENT_USER\Software\Classes and HKEY_LOCAL_MACHINE\Software\Classes

我更新腳本包括建議的try /除外除了額外的反饋一些打印語句。

這是我如何更新腳本:

""" 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) 
相關問題