{"id":1057100,"date":"2024-12-31T15:04:57","date_gmt":"2024-12-31T07:04:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1057100.html"},"modified":"2024-12-31T15:04:57","modified_gmt":"2024-12-31T07:04:57","slug":"%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%aepython%e6%96%87%e4%bb%b6%e7%9a%84%e9%bb%98%e8%ae%a4%e5%b7%a5%e4%bd%9c%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1057100.html","title":{"rendered":"\u5982\u4f55\u8bbe\u7f6epython\u6587\u4ef6\u7684\u9ed8\u8ba4\u5de5\u4f5c\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/3671737f-4867-4995-9281-5307c8b89172.webp\" alt=\"\u5982\u4f55\u8bbe\u7f6epython\u6587\u4ef6\u7684\u9ed8\u8ba4\u5de5\u4f5c\u8def\u5f84\" \/><\/p>\n<p><p> <strong>\u8bbe\u7f6ePython\u6587\u4ef6\u7684\u9ed8\u8ba4\u5de5\u4f5c\u8def\u5f84\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528sys\u6a21\u5757\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u3002\u4f7f\u7528os\u6a21\u5757\u53ef\u4ee5\u7075\u6d3b\u5730\u83b7\u53d6\u548c\u8bbe\u7f6e\u5de5\u4f5c\u8def\u5f84\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757<\/h3>\n<\/p>\n<p><p><strong>os\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u4e13\u95e8\u7528\u4e8e\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u3002os.chdir()\u51fd\u6570\u7528\u4e8e\u66f4\u6539\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8bbe\u5b9a\u9ed8\u8ba4\u5de5\u4f5c\u8def\u5f84<\/strong><\/h2>\n<p>default_path = &quot;\/path\/to\/your\/directory&quot;<\/p>\n<p>os.chdir(default_path)<\/p>\n<h2><strong>\u9a8c\u8bc1\u5f53\u524d\u5de5\u4f5c\u8def\u5f84<\/strong><\/h2>\n<p>print(&quot;Current Working Directory:&quot;, os.getcwd())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5f53\u524d\u7684\u5de5\u4f5c\u8def\u5f84\u8bbe\u7f6e\u4e3a\u6307\u5b9a\u7684\u8def\u5f84\uff0c\u8fd9\u5bf9\u8bfb\u53d6\u6587\u4ef6\u548c\u4fdd\u5b58\u6587\u4ef6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528sys\u6a21\u5757<\/h3>\n<\/p>\n<p><p><strong>sys\u6a21\u5757\u662f\u53e6\u4e00\u4e2aPython\u5185\u7f6e\u7684\u6807\u51c6\u5e93\uff0csys.path\u5217\u8868\u5b58\u50a8\u4e86Python\u89e3\u91ca\u5668\u67e5\u627e\u6a21\u5757\u7684\u8def\u5f84\u3002\u6211\u4eec\u53ef\u4ee5\u5411sys.path\u4e2d\u6dfb\u52a0\u81ea\u5b9a\u4e49\u8def\u5f84\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u8bbe\u5b9a\u9ed8\u8ba4\u5de5\u4f5c\u8def\u5f84<\/strong><\/h2>\n<p>default_path = &quot;\/path\/to\/your\/directory&quot;<\/p>\n<p>sys.path.append(default_path)<\/p>\n<h2><strong>\u9a8c\u8bc1\u662f\u5426\u6dfb\u52a0\u6210\u529f<\/strong><\/h2>\n<p>import os<\/p>\n<p>if default_path in sys.path:<\/p>\n<p>    print(&quot;Path successfully added!&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to add path.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528sys\u6a21\u5757\u7684\u597d\u5904\u662f\uff0c\u6dfb\u52a0\u5230sys.path\u4e2d\u7684\u8def\u5f84\u53ef\u4ee5\u5f71\u54cd\u6a21\u5757\u7684\u5bfc\u5165\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u662f\u4e00\u79cd\u66f4\u52a0\u7075\u6d3b\u548c\u6301\u4e45\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u6765\u5b9a\u4e49\u5de5\u4f5c\u8def\u5f84\uff0c\u5e76\u5728Python\u811a\u672c\u4e2d\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u5185\u5bb9\u3002<\/strong><\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u914d\u7f6e\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6\uff08\u4f8b\u5982config.ini\uff09\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-ini\">[DEFAULT]<\/p>\n<p>WorkingDirectory = \/path\/to\/your\/directory<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u8bfb\u53d6\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import configparser<\/p>\n<h2><strong>\u521b\u5efa\u914d\u7f6e\u89e3\u6790\u5668<\/strong><\/h2>\n<p>config = configparser.ConfigParser()<\/p>\n<p>config.read(&#39;config.ini&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u5de5\u4f5c\u8def\u5f84<\/strong><\/h2>\n<p>default_path = config[&#39;DEFAULT&#39;][&#39;WorkingDirectory&#39;]<\/p>\n<p>os.chdir(default_path)<\/p>\n<h2><strong>\u9a8c\u8bc1\u5f53\u524d\u5de5\u4f5c\u8def\u5f84<\/strong><\/h2>\n<p>print(&quot;Current Working Directory:&quot;, os.getcwd())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u7684\u597d\u5904\u662f\uff0c\u53ef\u4ee5\u5c06\u8def\u5f84\u4fe1\u606f\u4e0e\u4ee3\u7801\u5206\u79bb\uff0c\u66f4\u52a0\u6613\u4e8e\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u76f8\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u53ef\u80fd\u66f4\u52a0\u65b9\u4fbf\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7os.path\u6a21\u5757\u6765\u83b7\u53d6\u811a\u672c\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u5e76\u57fa\u4e8e\u6b64\u8bbe\u7f6e\u5de5\u4f5c\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u811a\u672c\u7684\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>script_dir = os.path.dirname(os.path.abspath(__file__))<\/p>\n<h2><strong>\u8bbe\u5b9a\u5de5\u4f5c\u8def\u5f84\u4e3a\u5f53\u524d\u811a\u672c\u6240\u5728\u76ee\u5f55<\/strong><\/h2>\n<p>os.chdir(script_dir)<\/p>\n<h2><strong>\u9a8c\u8bc1\u5f53\u524d\u5de5\u4f5c\u8def\u5f84<\/strong><\/h2>\n<p>print(&quot;Current Working Directory:&quot;, os.getcwd())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5728IDE\u4e2d\u8bbe\u7f6e\u9ed8\u8ba4\u5de5\u4f5c\u8def\u5f84<\/h3>\n<\/p>\n<p><p><strong>\u4e0d\u540c\u7684\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u53ef\u80fd\u63d0\u4f9b\u4e86\u8bbe\u7f6e\u9ed8\u8ba4\u5de5\u4f5c\u8def\u5f84\u7684\u9009\u9879\u3002\u4f8b\u5982\uff0c\u5728PyCharm\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u201cFile -&gt; Settings -&gt; Project: [Project Name] -&gt; Project Structure\u201d\u6765\u8bbe\u7f6e\u9ed8\u8ba4\u5de5\u4f5c\u8def\u5f84\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u76f8\u5bf9\u8def\u5f84\u4e0e\u7edd\u5bf9\u8def\u5f84\uff1a<\/strong> \u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u66f4\u5177\u7075\u6d3b\u6027\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7edd\u5bf9\u8def\u5f84\u66f4\u52a0\u660e\u786e\u3002<\/li>\n<li><strong>\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\uff1a<\/strong> \u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u8868\u793a\u65b9\u5f0f\u4e0d\u540c\uff0c\u5efa\u8bae\u4f7f\u7528os.path\u6a21\u5757\u6765\u5904\u7406\u8def\u5f84\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u3002<\/li>\n<li><strong>\u9519\u8bef\u5904\u7406\uff1a<\/strong> \u8bbe\u7f6e\u5de5\u4f5c\u8def\u5f84\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u8def\u5f84\u4e0d\u5b58\u5728\u6216\u6743\u9650\u4e0d\u8db3\u7b49\u95ee\u9898\uff0c\u5efa\u8bae\u6dfb\u52a0\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u4ee3\u7801\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>default_path = &quot;\/path\/to\/your\/directory&quot;<\/p>\n<p>try:<\/p>\n<p>    os.chdir(default_path)<\/p>\n<p>    print(&quot;Current Working Directory:&quot;, os.getcwd())<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&quot;The specified path does not exist.&quot;)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(&quot;Permission denied.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u8bbe\u7f6ePython\u6587\u4ef6\u7684\u9ed8\u8ba4\u5de5\u4f5c\u8def\u5f84\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001sys\u6a21\u5757\u3001\u914d\u7f6e\u6587\u4ef6\u7b49\u3002<\/strong> \u901a\u8fc7\u8bbe\u7f6e\u5de5\u4f5c\u8def\u5f84\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u65b9\u4fbf\u5730\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u9519\u8bef\u5904\u7406\u548c\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u7a33\u5065\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4fee\u6539\u9ed8\u8ba4\u5de5\u4f5c\u8def\u5f84\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u6765\u8bbe\u7f6e\u548c\u4fee\u6539\u5de5\u4f5c\u8def\u5f84\u3002\u901a\u8fc7<code>os.chdir(path)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u786e\u4fdd\u5728\u6267\u884c\u6b64\u64cd\u4f5c\u4e4b\u524d\u5bfc\u5165<code>os<\/code>\u6a21\u5757\uff0c\u5e76\u4e14\u63d0\u4f9b\u7684\u8def\u5f84\u662f\u6709\u6548\u7684\u3002\u5982\u679c\u4f60\u60f3\u8981\u5728\u6bcf\u6b21\u8fd0\u884c\u811a\u672c\u65f6\u90fd\u4f7f\u7528\u7279\u5b9a\u7684\u5de5\u4f5c\u8def\u5f84\uff0c\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u547d\u4ee4\u653e\u5728\u811a\u672c\u7684\u5f00\u5934\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u8def\u5f84\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>os.getcwd()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u5f53\u524d\u7684\u5de5\u4f5c\u8def\u5f84\u3002\u8fd9\u5728\u8c03\u8bd5\u65f6\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u786e\u8ba4\u5f53\u524d\u7684\u6267\u884c\u73af\u5883\u3002\u8bb0\u5f97\u5728\u4f7f\u7528\u4e4b\u524d\u5bfc\u5165<code>os<\/code>\u6a21\u5757\uff0c\u4ee5\u786e\u4fdd\u53ef\u4ee5\u8c03\u7528\u8be5\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728IDE\u4e2d\u8bbe\u7f6ePython\u811a\u672c\u7684\u5de5\u4f5c\u76ee\u5f55\uff1f<\/strong><br \/>\u5927\u591a\u6570\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u90fd\u5141\u8bb8\u4f60\u5728\u9879\u76ee\u8bbe\u7f6e\u4e2d\u914d\u7f6e\u5de5\u4f5c\u76ee\u5f55\u3002\u4f8b\u5982\uff0c\u5728PyCharm\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u201cRun\/Debug Configurations\u201d\u83dc\u5355\u6765\u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\u3002\u5728VS Code\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539<code>launch.json<\/code>\u6587\u4ef6\u4e2d\u7684<code>cwd<\/code>\u5c5e\u6027\u6765\u5b9e\u73b0\u3002\u8fd9\u4e9b\u8bbe\u7f6e\u4f1a\u5f71\u54cd\u811a\u672c\u8fd0\u884c\u65f6\u7684\u9ed8\u8ba4\u5de5\u4f5c\u8def\u5f84\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8bbe\u7f6ePython\u6587\u4ef6\u7684\u9ed8\u8ba4\u5de5\u4f5c\u8def\u5f84\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528sys\u6a21\u5757\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u3002\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1057100"}],"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=1057100"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1057100\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1057100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1057100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1057100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}