{"id":1032802,"date":"2024-12-31T11:36:40","date_gmt":"2024-12-31T03:36:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1032802.html"},"modified":"2024-12-31T11:36:42","modified_gmt":"2024-12-31T03:36:42","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e4%bf%ae%e6%94%b9%e6%b3%a8%e5%86%8c%e8%a1%a8%e4%b8%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1032802.html","title":{"rendered":"\u5982\u4f55\u7528python\u4fee\u6539\u6ce8\u518c\u8868\u4e0b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/b9d4e2ff-adfb-4445-b321-5f9801290f1c.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528python\u4fee\u6539\u6ce8\u518c\u8868\u4e0b\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u4fee\u6539\u6ce8\u518c\u8868\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528winreg\u6a21\u5757\u3001\u786e\u4fdd\u5177\u6709\u7ba1\u7406\u5458\u6743\u9650\u3001\u51c6\u786e\u6307\u5b9a\u6ce8\u518c\u8868\u8def\u5f84\u548c\u9879\u3001\u5907\u4efd\u6ce8\u518c\u8868\u7b49\u3002<\/strong> <\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u8be6\u7ec6\u89e3\u91ca\u5176\u4e2d\u4e00\u4e2a\u65b9\u6cd5\uff0c\u4f7f\u7528<code>winreg<\/code>\u6a21\u5757\u662f\u6700\u5e38\u89c1\u548c\u63a8\u8350\u7684\u65b9\u6cd5\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9Windows\u6ce8\u518c\u8868\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u53ef\u4ee5\u8bfb\u53d6\u3001\u521b\u5efa\u3001\u4fee\u6539\u548c\u5220\u9664\u6ce8\u518c\u8868\u9879\u548c\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Python\u4e2d\u7684winreg\u6a21\u5757\u6765\u4fee\u6539\u6ce8\u518c\u8868\uff1a<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5728\u8fd0\u884cPython\u811a\u672c\u65f6\u6709\u7ba1\u7406\u5458\u6743\u9650\uff0c\u56e0\u4e3a\u5bf9\u6ce8\u518c\u8868\u7684\u4fee\u6539\u53ef\u80fd\u4f1a\u5f71\u54cd\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import winreg as reg<\/p>\n<p>def modify_registry():<\/p>\n<p>    try:<\/p>\n<p>        # \u6253\u5f00\u6ce8\u518c\u8868\u9879<\/p>\n<p>        registry_key = reg.OpenKey(reg.HKEY_CURRENT_USER, r&quot;Software\\MyApp&quot;, 0, reg.KEY_WRITE)<\/p>\n<p>        # \u8bbe\u7f6e\u65b0\u7684\u503c<\/p>\n<p>        reg.SetValueEx(registry_key, &quot;MyValue&quot;, 0, reg.REG_SZ, &quot;NewValue&quot;)<\/p>\n<p>        # \u5173\u95ed\u6ce8\u518c\u8868\u9879<\/p>\n<p>        reg.CloseKey(registry_key)<\/p>\n<p>        print(&quot;\u6ce8\u518c\u8868\u4fee\u6539\u6210\u529f&quot;)<\/p>\n<p>    except WindowsError as e:<\/p>\n<p>        print(f&quot;\u4fee\u6539\u6ce8\u518c\u8868\u65f6\u51fa\u9519: {e}&quot;)<\/p>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    modify_registry()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u811a\u672c\u5c06\u5728<code>HKEY_CURRENT_USER\\Software\\MyApp<\/code>\u8def\u5f84\u4e0b\u521b\u5efa\u6216\u4fee\u6539\u540d\u4e3a\u201cMyValue\u201d\u7684\u6ce8\u518c\u8868\u9879\uff0c\u5e76\u5c06\u5176\u8bbe\u7f6e\u4e3a\u201cNewValue\u201d\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528winreg\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>winreg<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u4e13\u95e8\u7528\u4e8e\u5728Windows\u7cfb\u7edf\u4e0a\u64cd\u4f5c\u6ce8\u518c\u8868\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u6253\u5f00\u3001\u67e5\u8be2\u3001\u521b\u5efa\u548c\u5220\u9664\u6ce8\u518c\u8868\u9879\u548c\u503c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6253\u5f00\u6ce8\u518c\u8868\u9879<\/h4>\n<\/p>\n<p><p>\u5728\u64cd\u4f5c\u6ce8\u518c\u8868\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u5148\u6253\u5f00\u4e00\u4e2a\u6ce8\u518c\u8868\u9879\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>OpenKey<\/code>\u51fd\u6570\u6765\u5b8c\u6210\u8fd9\u9879\u4efb\u52a1\u3002\u8fd9\u4e2a\u51fd\u6570\u9700\u8981\u4e09\u4e2a\u53c2\u6570\uff1a\u6839\u952e\uff08\u4f8b\u5982<code>HKEY_CURRENT_USER<\/code>\uff09\u3001\u5b50\u952e\u8def\u5f84\u548c\u8bbf\u95ee\u6743\u9650\uff08\u4f8b\u5982<code>KEY_READ<\/code>\u6216<code>KEY_WRITE<\/code>\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import winreg as reg<\/p>\n<p>def open_key():<\/p>\n<p>    try:<\/p>\n<p>        # \u6253\u5f00\u6ce8\u518c\u8868\u9879<\/p>\n<p>        registry_key = reg.OpenKey(reg.HKEY_CURRENT_USER, r&quot;Software\\MyApp&quot;, 0, reg.KEY_READ)<\/p>\n<p>        print(&quot;\u6210\u529f\u6253\u5f00\u6ce8\u518c\u8868\u9879&quot;)<\/p>\n<p>        return registry_key<\/p>\n<p>    except WindowsError as e:<\/p>\n<p>        print(f&quot;\u6253\u5f00\u6ce8\u518c\u8868\u9879\u65f6\u51fa\u9519: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6\u6ce8\u518c\u8868\u503c<\/h4>\n<\/p>\n<p><p>\u8bfb\u53d6\u6ce8\u518c\u8868\u503c\u53ef\u4ee5\u4f7f\u7528<code>QueryValueEx<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a\u6253\u5f00\u7684\u6ce8\u518c\u8868\u9879\u548c\u8981\u8bfb\u53d6\u7684\u503c\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_value(registry_key):<\/p>\n<p>    try:<\/p>\n<p>        value, reg_type = reg.QueryValueEx(registry_key, &quot;MyValue&quot;)<\/p>\n<p>        print(f&quot;\u8bfb\u53d6\u5230\u7684\u503c: {value}&quot;)<\/p>\n<p>    except WindowsError as e:<\/p>\n<p>        print(f&quot;\u8bfb\u53d6\u6ce8\u518c\u8868\u503c\u65f6\u51fa\u9519: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4fee\u6539\u6ce8\u518c\u8868\u503c<\/h4>\n<\/p>\n<p><p>\u4fee\u6539\u6ce8\u518c\u8868\u503c\u4f7f\u7528<code>SetValueEx<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u9700\u8981\u4e94\u4e2a\u53c2\u6570\uff1a\u6253\u5f00\u7684\u6ce8\u518c\u8868\u9879\u3001\u503c\u7684\u540d\u79f0\u3001\u4fdd\u7559\uff08\u5fc5\u987b\u4e3a0\uff09\u3001\u503c\u7684\u7c7b\u578b\uff08\u4f8b\u5982<code>REG_SZ<\/code>\u8868\u793a\u5b57\u7b26\u4e32\uff09\u548c\u8981\u8bbe\u7f6e\u7684\u65b0\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def set_value(registry_key):<\/p>\n<p>    try:<\/p>\n<p>        reg.SetValueEx(registry_key, &quot;MyValue&quot;, 0, reg.REG_SZ, &quot;NewValue&quot;)<\/p>\n<p>        print(&quot;\u6ce8\u518c\u8868\u503c\u4fee\u6539\u6210\u529f&quot;)<\/p>\n<p>    except WindowsError as e:<\/p>\n<p>        print(f&quot;\u4fee\u6539\u6ce8\u518c\u8868\u503c\u65f6\u51fa\u9519: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u5173\u95ed\u6ce8\u518c\u8868\u9879<\/h4>\n<\/p>\n<p><p>\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u52a1\u5fc5\u5173\u95ed\u6ce8\u518c\u8868\u9879\uff0c\u4ee5\u91ca\u653e\u8d44\u6e90\u3002\u8fd9\u53ef\u4ee5\u4f7f\u7528<code>CloseKey<\/code>\u51fd\u6570\u6765\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def close_key(registry_key):<\/p>\n<p>    try:<\/p>\n<p>        reg.CloseKey(registry_key)<\/p>\n<p>        print(&quot;\u6ce8\u518c\u8868\u9879\u5173\u95ed\u6210\u529f&quot;)<\/p>\n<p>    except WindowsError as e:<\/p>\n<p>        print(f&quot;\u5173\u95ed\u6ce8\u518c\u8868\u9879\u65f6\u51fa\u9519: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u786e\u4fdd\u5177\u6709\u7ba1\u7406\u5458\u6743\u9650<\/h3>\n<\/p>\n<p><p>\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u4fee\u6539\u6ce8\u518c\u8868\u901a\u5e38\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fd0\u884cPython\u811a\u672c\u4ee5\u786e\u4fdd\u5177\u6709\u7ba1\u7406\u5458\u6743\u9650\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u6216PowerShell\u4e2d\uff0c\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\u3002<\/li>\n<li>\u8fd0\u884cPython\u811a\u672c\u3002<\/li>\n<\/ol>\n<p><p>\u6216\u8005\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>ctypes<\/code>\u5e93\u5728\u811a\u672c\u4e2d\u68c0\u67e5\u548c\u8bf7\u6c42\u7ba1\u7406\u5458\u6743\u9650\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ctypes<\/p>\n<p>import sys<\/p>\n<p>def is_admin():<\/p>\n<p>    try:<\/p>\n<p>        return ctypes.windll.shell32.IsUserAnAdmin()<\/p>\n<p>    except:<\/p>\n<p>        return False<\/p>\n<p>if is_admin():<\/p>\n<p>    # \u653e\u7f6e\u4f60\u7684\u6ce8\u518c\u8868\u4fee\u6539\u4ee3\u7801<\/p>\n<p>    modify_registry()<\/p>\n<p>else:<\/p>\n<p>    # \u5982\u679c\u6ca1\u6709\u7ba1\u7406\u5458\u6743\u9650\uff0c\u5219\u91cd\u65b0\u542f\u52a8\u811a\u672c\u5e76\u8bf7\u6c42\u7ba1\u7406\u5458\u6743\u9650<\/p>\n<p>    ctypes.windll.shell32.ShellExecuteW(None, &quot;runas&quot;, sys.executable, __file__, None, 1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5907\u4efd\u6ce8\u518c\u8868<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u4efb\u4f55\u4fee\u6539\u4e4b\u524d\uff0c\u5efa\u8bae\u5907\u4efd\u6ce8\u518c\u8868\u3002\u4f60\u53ef\u4ee5\u624b\u52a8\u5907\u4efd\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528Python\u811a\u672c\u6765\u5bfc\u51fa\u6ce8\u518c\u8868\u9879\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def backup_registry():<\/p>\n<p>    try:<\/p>\n<p>        subprocess.run([&quot;reg&quot;, &quot;export&quot;, r&quot;HKEY_CURRENT_USER\\Software\\MyApp&quot;, &quot;backup.reg&quot;, &quot;\/y&quot;], check=True)<\/p>\n<p>        print(&quot;\u6ce8\u518c\u8868\u5907\u4efd\u6210\u529f&quot;)<\/p>\n<p>    except subprocess.CalledProcessError as e:<\/p>\n<p>        print(f&quot;\u5907\u4efd\u6ce8\u518c\u8868\u65f6\u51fa\u9519: {e}&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    backup_registry()<\/p>\n<p>    modify_registry()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u64cd\u4f5c\u6ce8\u518c\u8868\u65f6\uff0c\u9519\u8bef\u548c\u5f02\u5e38\u5904\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<code>winreg<\/code>\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u5728\u9047\u5230\u9519\u8bef\u65f6\u4f1a\u629b\u51fa<code>WindowsError<\/code>\u3002\u4f60\u5e94\u8be5\u5728\u4ee3\u7801\u4e2d\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\u5e76\u8fdb\u884c\u9002\u5f53\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def modify_registry():<\/p>\n<p>    try:<\/p>\n<p>        # \u6253\u5f00\u6ce8\u518c\u8868\u9879<\/p>\n<p>        registry_key = reg.OpenKey(reg.HKEY_CURRENT_USER, r&quot;Software\\MyApp&quot;, 0, reg.KEY_WRITE)<\/p>\n<p>        # \u8bbe\u7f6e\u65b0\u7684\u503c<\/p>\n<p>        reg.SetValueEx(registry_key, &quot;MyValue&quot;, 0, reg.REG_SZ, &quot;NewValue&quot;)<\/p>\n<p>        # \u5173\u95ed\u6ce8\u518c\u8868\u9879<\/p>\n<p>        reg.CloseKey(registry_key)<\/p>\n<p>        print(&quot;\u6ce8\u518c\u8868\u4fee\u6539\u6210\u529f&quot;)<\/p>\n<p>    except WindowsError as e:<\/p>\n<p>        print(f&quot;\u4fee\u6539\u6ce8\u518c\u8868\u65f6\u51fa\u9519: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5220\u9664\u6ce8\u518c\u8868\u9879\u548c\u503c<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u5220\u9664\u6ce8\u518c\u8868\u9879\u6216\u503c\u3002\u53ef\u4ee5\u4f7f\u7528<code>DeleteKey<\/code>\u548c<code>DeleteValue<\/code>\u51fd\u6570\u6765\u5b8c\u6210\u8fd9\u9879\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5220\u9664\u6ce8\u518c\u8868\u503c<\/h4>\n<\/p>\n<p><p>\u5220\u9664\u6ce8\u518c\u8868\u503c\u4f7f\u7528<code>DeleteValue<\/code>\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a\u6253\u5f00\u7684\u6ce8\u518c\u8868\u9879\u548c\u8981\u5220\u9664\u7684\u503c\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def delete_value():<\/p>\n<p>    try:<\/p>\n<p>        registry_key = reg.OpenKey(reg.HKEY_CURRENT_USER, r&quot;Software\\MyApp&quot;, 0, reg.KEY_WRITE)<\/p>\n<p>        reg.DeleteValue(registry_key, &quot;MyValue&quot;)<\/p>\n<p>        reg.CloseKey(registry_key)<\/p>\n<p>        print(&quot;\u6ce8\u518c\u8868\u503c\u5220\u9664\u6210\u529f&quot;)<\/p>\n<p>    except WindowsError as e:<\/p>\n<p>        print(f&quot;\u5220\u9664\u6ce8\u518c\u8868\u503c\u65f6\u51fa\u9519: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5220\u9664\u6ce8\u518c\u8868\u9879<\/h4>\n<\/p>\n<p><p>\u5220\u9664\u6ce8\u518c\u8868\u9879\u4f7f\u7528<code>DeleteKey<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a\u6839\u952e\u548c\u8981\u5220\u9664\u7684\u5b50\u952e\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def delete_key():<\/p>\n<p>    try:<\/p>\n<p>        reg.DeleteKey(reg.HKEY_CURRENT_USER, r&quot;Software\\MyApp&quot;)<\/p>\n<p>        print(&quot;\u6ce8\u518c\u8868\u9879\u5220\u9664\u6210\u529f&quot;)<\/p>\n<p>    except WindowsError as e:<\/p>\n<p>        print(f&quot;\u5220\u9664\u6ce8\u518c\u8868\u9879\u65f6\u51fa\u9519: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u521b\u5efa\u65b0\u7684\u6ce8\u518c\u8868\u9879\u548c\u503c<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u521b\u5efa\u65b0\u7684\u6ce8\u518c\u8868\u9879\u548c\u503c\u3002\u53ef\u4ee5\u4f7f\u7528<code>CreateKey<\/code>\u548c<code>SetValueEx<\/code>\u51fd\u6570\u6765\u5b8c\u6210\u8fd9\u9879\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u6ce8\u518c\u8868\u9879<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u6ce8\u518c\u8868\u9879\u4f7f\u7528<code>CreateKey<\/code>\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a\u6839\u952e\u548c\u8981\u521b\u5efa\u7684\u5b50\u952e\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_key():<\/p>\n<p>    try:<\/p>\n<p>        registry_key = reg.CreateKey(reg.HKEY_CURRENT_USER, r&quot;Software\\MyNewApp&quot;)<\/p>\n<p>        print(&quot;\u6ce8\u518c\u8868\u9879\u521b\u5efa\u6210\u529f&quot;)<\/p>\n<p>        return registry_key<\/p>\n<p>    except WindowsError as e:<\/p>\n<p>        print(f&quot;\u521b\u5efa\u6ce8\u518c\u8868\u9879\u65f6\u51fa\u9519: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bbe\u7f6e\u6ce8\u518c\u8868\u503c<\/h4>\n<\/p>\n<p><p>\u8bbe\u7f6e\u6ce8\u518c\u8868\u503c\u4f7f\u7528<code>SetValueEx<\/code>\u51fd\u6570\uff0c\u5982\u524d\u6240\u8ff0\u3002\u4f60\u53ef\u4ee5\u5728\u521b\u5efa\u6ce8\u518c\u8868\u9879\u540e\u7acb\u5373\u8bbe\u7f6e\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_and_set_value():<\/p>\n<p>    registry_key = create_key()<\/p>\n<p>    if registry_key:<\/p>\n<p>        try:<\/p>\n<p>            reg.SetValueEx(registry_key, &quot;MyNewValue&quot;, 0, reg.REG_SZ, &quot;InitialValue&quot;)<\/p>\n<p>            print(&quot;\u6ce8\u518c\u8868\u503c\u8bbe\u7f6e\u6210\u529f&quot;)<\/p>\n<p>        except WindowsError as e:<\/p>\n<p>            print(f&quot;\u8bbe\u7f6e\u6ce8\u518c\u8868\u503c\u65f6\u51fa\u9519: {e}&quot;)<\/p>\n<p>        finally:<\/p>\n<p>            reg.CloseKey(registry_key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u4fee\u6539\u6ce8\u518c\u8868\u65f6\uff0c\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u786e\u4fdd\u5177\u6709\u7ba1\u7406\u5458\u6743\u9650<\/strong>\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u4e0d\u8db3\u7684\u9519\u8bef\u3002<\/li>\n<li><strong>\u5907\u4efd\u6ce8\u518c\u8868<\/strong>\uff0c\u4ee5\u9632\u4fee\u6539\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u95ee\u9898\u3002<\/li>\n<li>\u4f7f\u7528<code>winreg<\/code>\u6a21\u5757\u63d0\u4f9b\u7684\u51fd\u6570\u6765<strong>\u6253\u5f00\u3001\u8bfb\u53d6\u3001\u4fee\u6539\u3001\u5220\u9664\u548c\u521b\u5efa\u6ce8\u518c\u8868\u9879\u548c\u503c<\/strong>\u3002<\/li>\n<li><strong>\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38<\/strong>\uff0c\u786e\u4fdd\u5728\u51fa\u73b0\u95ee\u9898\u65f6\u811a\u672c\u4e0d\u4f1a\u5d29\u6e83\uff0c\u5e76\u63d0\u4f9b\u9002\u5f53\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<li><strong>\u5173\u95ed\u6ce8\u518c\u8868\u9879<\/strong>\uff0c\u4ee5\u91ca\u653e\u8d44\u6e90\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u4f60\u53ef\u4ee5\u5b89\u5168\u6709\u6548\u5730\u4f7f\u7528Python\u6765\u4fee\u6539Windows\u6ce8\u518c\u8868\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u5e76\u795d\u4f60\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\u4e00\u5207\u987a\u5229\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u901a\u8fc7Python\u5b89\u5168\u5730\u4fee\u6539Windows\u6ce8\u518c\u8868\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u4fee\u6539\u6ce8\u518c\u8868\u4e4b\u524d\uff0c\u786e\u4fdd\u5907\u4efd\u6ce8\u518c\u8868\u6570\u636e\uff0c\u4ee5\u9632\u51fa\u73b0\u610f\u5916\u60c5\u51b5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>winreg<\/code>\u6a21\u5757\u6765\u8bfb\u53d6\u548c\u4fee\u6539\u6ce8\u518c\u8868\u9879\u3002\u8bf7\u53c2\u8003\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u6765\u4e86\u89e3\u5982\u4f55\u5b89\u5168\u5730\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<p><strong>\u4fee\u6539\u6ce8\u518c\u8868\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6743\u9650\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u4fee\u6539\u6ce8\u518c\u8868\u65f6\uff0c\u786e\u4fdd\u4f60\u7684Python\u811a\u672c\u4ee5\u7ba1\u7406\u5458\u6743\u9650\u8fd0\u884c\uff0c\u56e0\u4e3a\u67d0\u4e9b\u6ce8\u518c\u8868\u9879\u7684\u4fee\u6539\u9700\u8981\u66f4\u9ad8\u7684\u6743\u9650\u3002\u5982\u679c\u6ca1\u6709\u8db3\u591f\u7684\u6743\u9650\uff0c\u4fee\u6539\u64cd\u4f5c\u53ef\u80fd\u4f1a\u5931\u8d25\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u4fee\u6539\u6ce8\u518c\u8868\u65f6\uff0c\u5982\u4f55\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6ce8\u518c\u8868\u64cd\u4f5c\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u53ef\u80fd\u7684\u5f02\u5e38\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u51fa\u73b0\u9519\u8bef\u65f6\uff0c\u7a0b\u5e8f\u4e0d\u4f1a\u5d29\u6e83\uff0c\u5e76\u4e14\u80fd\u591f\u9002\u5f53\u5730\u5904\u7406\u9519\u8bef\uff0c\u4f8b\u5982\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u6216\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\u3002 <\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u901a\u8fc7Python\u811a\u672c\u67e5\u770b\u6ce8\u518c\u8868\u7684\u6240\u6709\u952e\u503c\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528Python\u53ef\u4ee5\u904d\u5386\u6ce8\u518c\u8868\u5e76\u67e5\u770b\u6240\u6709\u7684\u952e\u503c\u3002\u901a\u8fc7<code>winreg<\/code>\u6a21\u5757\u4e2d\u7684<code>EnumKey<\/code>\u548c<code>EnumValue<\/code>\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u5217\u51fa\u6307\u5b9a\u8def\u5f84\u4e0b\u7684\u6240\u6709\u5b50\u952e\u548c\u952e\u503c\uff0c\u4ece\u800c\u66f4\u5168\u9762\u5730\u4e86\u89e3\u6ce8\u518c\u8868\u7684\u7ed3\u6784\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u4fee\u6539\u6ce8\u518c\u8868\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528winreg\u6a21\u5757\u3001\u786e\u4fdd\u5177\u6709\u7ba1\u7406\u5458\u6743\u9650\u3001\u51c6\u786e\u6307\u5b9a\u6ce8\u518c\u8868\u8def\u5f84\u548c\u9879\u3001\u5907\u4efd\u6ce8\u518c [&hellip;]","protected":false},"author":3,"featured_media":1032806,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032802"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1032802"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032802\/revisions"}],"predecessor-version":[{"id":1032808,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032802\/revisions\/1032808"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1032806"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1032802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1032802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1032802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}