{"id":1177184,"date":"2025-01-15T17:55:07","date_gmt":"2025-01-15T09:55:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1177184.html"},"modified":"2025-01-15T17:55:09","modified_gmt":"2025-01-15T09:55:09","slug":"%e5%a6%82%e4%bd%95%e6%94%b9%e5%8f%98python%e7%9a%84%e9%bb%98%e8%ae%a4%e7%9b%ae%e5%bd%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1177184.html","title":{"rendered":"\u5982\u4f55\u6539\u53d8python\u7684\u9ed8\u8ba4\u76ee\u5f55"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25112144\/23efd0b1-e669-4e2e-951f-7eb32d3aa672.webp\" alt=\"\u5982\u4f55\u6539\u53d8python\u7684\u9ed8\u8ba4\u76ee\u5f55\" \/><\/p>\n<p><p> <strong>\u6539\u53d8Python\u9ed8\u8ba4\u76ee\u5f55\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528os\u6a21\u5757\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3001\u66f4\u6539Python\u811a\u672c\u7684\u542f\u52a8\u8def\u5f84\u3002<\/strong> <\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>\u4f7f\u7528os\u6a21\u5757<\/strong>\u662f\u6700\u5e38\u89c1\u548c\u7075\u6d3b\u7684\u65b9\u6cd5\u3002\u901a\u8fc7os\u6a21\u5757\u4e2d\u7684<code>chdir<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u66f4\u6539\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528os\u6a21\u5757\u6539\u53d8\u9ed8\u8ba4\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528os\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>os<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>os.chdir()<\/code>\u51fd\u6570\u6765\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6253\u5370\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, os.getcwd())<\/p>\n<h2><strong>\u6539\u53d8\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>os.chdir(&#39;\/path\/to\/new\/directory&#39;)<\/p>\n<h2><strong>\u6253\u5370\u65b0\u7684\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>print(&quot;\u65b0\u7684\u5de5\u4f5c\u76ee\u5f55:&quot;, os.getcwd())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u4f7f\u7528<code>os.getcwd()<\/code>\u51fd\u6570\u6253\u5370\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u7136\u540e\u4f7f\u7528<code>os.chdir()<\/code>\u51fd\u6570\u5c06\u5de5\u4f5c\u76ee\u5f55\u66f4\u6539\u4e3a\u6307\u5b9a\u8def\u5f84\uff0c\u6700\u540e\u518d\u6b21\u4f7f\u7528<code>os.getcwd()<\/code>\u51fd\u6570\u6253\u5370\u65b0\u7684\u5de5\u4f5c\u76ee\u5f55\u4ee5\u9a8c\u8bc1\u66f4\u6539\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1\u3001\u83b7\u53d6\u548c\u8bbe\u7f6e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>os.getcwd()<\/code>\u51fd\u6570\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u5e76\u4f7f\u7528<code>os.chdir()<\/code>\u51fd\u6570\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_dir = os.getcwd()<\/p>\n<p>print(f&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55: {current_dir}&quot;)<\/p>\n<h2><strong>\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>new_dir = &#39;\/path\/to\/new\/directory&#39;<\/p>\n<p>os.chdir(new_dir)<\/p>\n<h2><strong>\u9a8c\u8bc1\u5de5\u4f5c\u76ee\u5f55\u662f\u5426\u66f4\u6539\u6210\u529f<\/strong><\/h2>\n<p>current_dir = os.getcwd()<\/p>\n<p>print(f&quot;\u65b0\u7684\u5de5\u4f5c\u76ee\u5f55: {current_dir}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u4f7f\u7528<code>os.getcwd()<\/code>\u83b7\u53d6\u5e76\u6253\u5370\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528<code>os.chdir()<\/code>\u5c06\u5de5\u4f5c\u76ee\u5f55\u66f4\u6539\u4e3a\u6307\u5b9a\u8def\u5f84\u3002\u6700\u540e\uff0c\u518d\u6b21\u4f7f\u7528<code>os.getcwd()<\/code>\u83b7\u53d6\u5e76\u6253\u5370\u65b0\u7684\u5de5\u4f5c\u76ee\u5f55\u4ee5\u9a8c\u8bc1\u66f4\u6539\u662f\u5426\u6210\u529f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001os\u6a21\u5757\u7684\u5176\u4ed6\u76f8\u5173\u529f\u80fd<\/h4>\n<\/p>\n<p><p>\u9664\u4e86<code>os.getcwd()<\/code>\u548c<code>os.chdir()<\/code>\uff0c<code>os<\/code>\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u4e00\u4e9b\u4e0e\u76ee\u5f55\u76f8\u5173\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>os.listdir(path)<\/code>: \u5217\u51fa\u6307\u5b9a\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55<\/li>\n<li><code>os.mkdir(path)<\/code>: \u521b\u5efa\u65b0\u76ee\u5f55<\/li>\n<li><code>os.rmdir(path)<\/code>: \u5220\u9664\u7a7a\u76ee\u5f55<\/li>\n<li><code>os.path.join(path, *paths)<\/code>: \u5c06\u591a\u4e2a\u8def\u5f84\u7ec4\u5408\u6210\u4e00\u4e2a\u8def\u5f84<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528<code>os.listdir()<\/code>\u5217\u51fa\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5217\u51fa\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55<\/strong><\/h2>\n<p>files_and_dirs = os.listdir(&#39;.&#39;)<\/p>\n<p>print(f&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55: {files_and_dirs}&quot;)<\/p>\n<h2><strong>\u5217\u51fa\u6307\u5b9a\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55<\/strong><\/h2>\n<p>specified_dir = &#39;\/path\/to\/specified\/directory&#39;<\/p>\n<p>files_and_dirs = os.listdir(specified_dir)<\/p>\n<p>print(f&quot;{specified_dir} \u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55: {files_and_dirs}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u6539\u53d8\u9ed8\u8ba4\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u6765\u6539\u53d8Python\u7684\u9ed8\u8ba4\u5de5\u4f5c\u76ee\u5f55\u3002\u53ef\u4ee5\u5728\u64cd\u4f5c\u7cfb\u7edf\u7ea7\u522b\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u4ece\u800c\u5728\u542f\u52a8Python\u811a\u672c\u65f6\u81ea\u52a8\u5c06\u5de5\u4f5c\u76ee\u5f55\u8bbe\u7f6e\u4e3a\u6307\u5b9a\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5728Linux\u6216macOS\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export PYTHONSTARTUP=\/path\/to\/startup\/script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Windows\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u201c\u63a7\u5236\u9762\u677f\u201d-&gt;\u201c\u7cfb\u7edf\u548c\u5b89\u5168\u201d-&gt;\u201c\u7cfb\u7edf\u201d-&gt;\u201c\u9ad8\u7ea7\u7cfb\u7edf\u8bbe\u7f6e\u201d\u3002<\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u5c5e\u6027\u201d\u5bf9\u8bdd\u6846\u4e2d\uff0c\u70b9\u51fb\u201c\u73af\u5883\u53d8\u91cf\u201d\u6309\u94ae\u3002<\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u53d8\u91cf\u201d\u90e8\u5206\uff0c\u70b9\u51fb\u201c\u65b0\u5efa\u201d\u6309\u94ae\uff0c\u521b\u5efa\u65b0\u7684\u73af\u5883\u53d8\u91cf\u3002<\/li>\n<li>\u8f93\u5165\u53d8\u91cf\u540d\u4e3a<code>PYTHONSTARTUP<\/code>\uff0c\u53d8\u91cf\u503c\u4e3a<code>\/path\/to\/startup\/script.py<\/code>\u3002<\/li>\n<li>\u70b9\u51fb\u201c\u786e\u5b9a\u201d\u6309\u94ae\u4fdd\u5b58\u8bbe\u7f6e\u3002<\/li>\n<\/ol>\n<p><h4>2\u3001\u5728\u542f\u52a8\u811a\u672c\u4e2d\u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u542f\u52a8\u811a\u672c<code>\/path\/to\/startup\/script.py<\/code>\uff0c\u5728\u5176\u4e2d\u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>new_dir = &#39;\/path\/to\/new\/directory&#39;<\/p>\n<p>os.chdir(new_dir)<\/p>\n<p>print(f&quot;\u5de5\u4f5c\u76ee\u5f55\u5df2\u66f4\u6539\u4e3a: {os.getcwd()}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6bcf\u6b21\u542f\u52a8Python\u89e3\u91ca\u5668\u65f6\uff0c\u90fd\u4f1a\u81ea\u52a8\u6267\u884c\u542f\u52a8\u811a\u672c\u4e2d\u7684\u4ee3\u7801\uff0c\u5e76\u5c06\u5de5\u4f5c\u76ee\u5f55\u8bbe\u7f6e\u4e3a\u6307\u5b9a\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u66f4\u6539Python\u811a\u672c\u7684\u542f\u52a8\u8def\u5f84<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u811a\u672c\u542f\u52a8\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u5728\u542f\u52a8Python\u811a\u672c\u65f6\u6307\u5b9a\u5de5\u4f5c\u76ee\u5f55\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u4e2d\u66f4\u6539\u76ee\u5f55\u5e76\u542f\u52a8\u811a\u672c\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">cd \/path\/to\/new\/directory<\/p>\n<p>python script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u4e34\u65f6\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55\uff0c\u800c\u4e0d\u9700\u8981\u4fee\u6539\u811a\u672c\u672c\u8eab\u6216\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5728\u811a\u672c\u4e2d\u52a8\u6001\u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.chdir()<\/code>\u51fd\u6570\u52a8\u6001\u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>new_dir = &#39;\/path\/to\/new\/directory&#39;<\/p>\n<p>os.chdir(new_dir)<\/p>\n<p>print(f&quot;\u5de5\u4f5c\u76ee\u5f55\u5df2\u66f4\u6539\u4e3a: {os.getcwd()}&quot;)<\/p>\n<h2><strong>\u811a\u672c\u7684\u5176\u4ed6\u4ee3\u7801<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728\u811a\u672c\u6267\u884c\u8fc7\u7a0b\u4e2d\u52a8\u6001\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u914d\u7f6e\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u6765\u5b58\u50a8\u5de5\u4f5c\u76ee\u5f55\u8def\u5f84\u3002\u53ef\u4ee5\u5728\u811a\u672c\u4e2d\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u5e76\u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6<code>config.ini<\/code>\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-ini\">[Settings]<\/p>\n<p>work_dir = \/path\/to\/new\/directory<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u5e76\u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\uff0c\u4f7f\u7528<code>configparser<\/code>\u6a21\u5757\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u5e76\u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import configparser<\/p>\n<h2><strong>\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6<\/strong><\/h2>\n<p>config = configparser.ConfigParser()<\/p>\n<p>config.read(&#39;config.ini&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u5de5\u4f5c\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>work_dir = config[&#39;Settings&#39;][&#39;work_dir&#39;]<\/p>\n<h2><strong>\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>os.chdir(work_dir)<\/p>\n<p>print(f&quot;\u5de5\u4f5c\u76ee\u5f55\u5df2\u66f4\u6539\u4e3a: {os.getcwd()}&quot;)<\/p>\n<h2><strong>\u811a\u672c\u7684\u5176\u4ed6\u4ee3\u7801<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u5de5\u4f5c\u76ee\u5f55\u8def\u5f84\u5b58\u50a8\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u5e76\u5728\u811a\u672c\u4e2d\u52a8\u6001\u8bfb\u53d6\u548c\u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u7528\u4e8e\u5728\u4ee3\u7801\u5757\u6267\u884c\u65f6\u4e34\u65f6\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55\u3002\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u53ef\u4ee5\u786e\u4fdd\u5728\u4ee3\u7801\u5757\u6267\u884c\u5b8c\u6bd5\u540e\u6062\u590d\u539f\u59cb\u5de5\u4f5c\u76ee\u5f55\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from contextlib import contextmanager<\/p>\n<p>@contextmanager<\/p>\n<p>def change_work_dir(new_dir):<\/p>\n<p>    # \u4fdd\u5b58\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/p>\n<p>    original_dir = os.getcwd()<\/p>\n<p>    try:<\/p>\n<p>        # \u66f4\u6539\u5de5\u4f5c\u76ee\u5f55<\/p>\n<p>        os.chdir(new_dir)<\/p>\n<p>        yield<\/p>\n<p>    finally:<\/p>\n<p>        # \u6062\u590d\u539f\u59cb\u5de5\u4f5c\u76ee\u5f55<\/p>\n<p>        os.chdir(original_dir)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p><p>\u5728\u811a\u672c\u4e2d\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4e34\u65f6\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/strong><\/h2>\n<p>@contextmanager<\/p>\n<p>def change_work_dir(new_dir):<\/p>\n<p>    original_dir = os.getcwd()<\/p>\n<p>    try:<\/p>\n<p>        os.chdir(new_dir)<\/p>\n<p>        yield<\/p>\n<p>    finally:<\/p>\n<p>        os.chdir(original_dir)<\/p>\n<h2><strong>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/strong><\/h2>\n<p>with change_work_dir(&#39;\/path\/to\/new\/directory&#39;):<\/p>\n<p>    print(f&quot;\u4e34\u65f6\u5de5\u4f5c\u76ee\u5f55: {os.getcwd()}&quot;)<\/p>\n<p>    # \u5728\u6b64\u5904\u6267\u884c\u9700\u8981\u5728\u65b0\u76ee\u5f55\u4e2d\u8fd0\u884c\u7684\u4ee3\u7801<\/p>\n<h2><strong>\u6062\u590d\u539f\u59cb\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>print(f&quot;\u6062\u590d\u539f\u59cb\u5de5\u4f5c\u76ee\u5f55: {os.getcwd()}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u5757\u6267\u884c\u65f6\u4e34\u65f6\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55\uff0c\u5e76\u5728\u4ee3\u7801\u5757\u6267\u884c\u5b8c\u6bd5\u540e\u81ea\u52a8\u6062\u590d\u539f\u59cb\u5de5\u4f5c\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u8def\u5f84\u5e93<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528Pathlib\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u9664\u4e86<code>os<\/code>\u6a21\u5757\u5916\uff0cPython\u8fd8\u63d0\u4f9b\u4e86<code>pathlib<\/code>\u6a21\u5757\uff0c\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u8def\u5f84\u3002<code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u52a0\u9762\u5411\u5bf9\u8c61\u7684\u63a5\u53e3\uff0c\u4f7f\u5f97\u8def\u5f84\u64cd\u4f5c\u66f4\u52a0\u7b80\u6d01\u548c\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u6253\u5370\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>print(f&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55: {Path.cwd()}&quot;)<\/p>\n<h2><strong>\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>new_dir = Path(&#39;\/path\/to\/new\/directory&#39;)<\/p>\n<p>new_dir.chdir()<\/p>\n<h2><strong>\u6253\u5370\u65b0\u7684\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>print(f&quot;\u65b0\u7684\u5de5\u4f5c\u76ee\u5f55: {Path.cwd()}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u4f7f\u7528<code>Path.cwd()<\/code>\u83b7\u53d6\u5e76\u6253\u5370\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528<code>Path<\/code>\u7c7b\u521b\u5efa\u65b0\u76ee\u5f55\u8def\u5f84\uff0c\u5e76\u8c03\u7528<code>chdir()<\/code>\u65b9\u6cd5\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55\u3002\u6700\u540e\uff0c\u518d\u6b21\u4f7f\u7528<code>Path.cwd()<\/code>\u83b7\u53d6\u5e76\u6253\u5370\u65b0\u7684\u5de5\u4f5c\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h4>2\u3001Pathlib\u6a21\u5757\u7684\u5176\u4ed6\u529f\u80fd<\/h4>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u6709\u7528\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u521b\u5efa\u76ee\u5f55\u3001\u5217\u51fa\u76ee\u5f55\u5185\u5bb9\u3001\u68c0\u67e5\u6587\u4ef6\u6216\u76ee\u5f55\u662f\u5426\u5b58\u5728\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u529f\u80fd\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u65b0\u76ee\u5f55<\/strong><\/h2>\n<p>new_dir = Path(&#39;\/path\/to\/new\/directory&#39;)<\/p>\n<p>new_dir.mkdir(parents=True, exist_ok=True)<\/p>\n<h2><strong>\u5217\u51fa\u76ee\u5f55\u5185\u5bb9<\/strong><\/h2>\n<p>print(f&quot;{new_dir} \u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55: {list(new_dir.iterdir())}&quot;)<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u6216\u76ee\u5f55\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if new_dir.exists():<\/p>\n<p>    print(f&quot;{new_dir} \u5b58\u5728&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{new_dir} \u4e0d\u5b58\u5728&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u66f4\u52a0\u7b80\u6d01\u548c\u76f4\u89c2\u5730\u5904\u7406\u8def\u5f84\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u66f4\u6539Python\u9ed8\u8ba4\u76ee\u5f55\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u5b58\u50a8\u5de5\u4f5c\u76ee\u5f55\u8def\u5f84\uff0c\u5e76\u5728\u811a\u672c\u4e2d\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u548c\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4e34\u65f6\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import configparser<\/p>\n<p>from contextlib import contextmanager<\/p>\n<h2><strong>\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6<\/strong><\/h2>\n<p>config = configparser.ConfigParser()<\/p>\n<p>config.read(&#39;config.ini&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u5de5\u4f5c\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>work_dir = config[&#39;Settings&#39;][&#39;work_dir&#39;]<\/p>\n<h2><strong>\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/strong><\/h2>\n<p>@contextmanager<\/p>\n<p>def change_work_dir(new_dir):<\/p>\n<p>    original_dir = os.getcwd()<\/p>\n<p>    try:<\/p>\n<p>        os.chdir(new_dir)<\/p>\n<p>        yield<\/p>\n<p>    finally:<\/p>\n<p>        os.chdir(original_dir)<\/p>\n<h2><strong>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/strong><\/h2>\n<p>with change_work_dir(work_dir):<\/p>\n<p>    print(f&quot;\u4e34\u65f6\u5de5\u4f5c\u76ee\u5f55: {os.getcwd()}&quot;)<\/p>\n<p>    # \u5728\u6b64\u5904\u6267\u884c\u9700\u8981\u5728\u65b0\u76ee\u5f55\u4e2d\u8fd0\u884c\u7684\u4ee3\u7801<\/p>\n<h2><strong>\u6062\u590d\u539f\u59cb\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>print(f&quot;\u6062\u590d\u539f\u59cb\u5de5\u4f5c\u76ee\u5f55: {os.getcwd()}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5b9e\u73b0\u66f4\u6539Python\u9ed8\u8ba4\u76ee\u5f55\u7684\u9700\u6c42\uff0c\u5e76\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u6539\u53d8Python\u9ed8\u8ba4\u76ee\u5f55\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3001\u66f4\u6539Python\u811a\u672c\u7684\u542f\u52a8\u8def\u5f84\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4ee5\u53ca\u4f7f\u7528pathlib\u6a21\u5757\u7b49\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528os\u6a21\u5757\u662f\u6700\u5e38\u89c1\u548c\u7075\u6d3b\u7684\u65b9\u6cd5<\/strong>\uff0c\u901a\u8fc7<code>os.chdir()<\/code>\u51fd\u6570\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u66f4\u6539\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u800c<strong>\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/strong>\u548c<strong>\u66f4\u6539Python\u811a\u672c\u7684\u542f\u52a8\u8def\u5f84<\/strong>\u5219\u9002\u7528\u4e8e\u5728\u811a\u672c\u542f\u52a8\u65f6\u81ea\u52a8\u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\u3002\u6b64\u5916\uff0c<strong>\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6<\/strong>\u548c<strong>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/strong>\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u800c<strong>pathlib\u6a21\u5757<\/strong>\u63d0\u4f9b\u4e86\u66f4\u52a0\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u751a\u81f3\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u66f4\u6539Python\u9ed8\u8ba4\u76ee\u5f55\u7684\u9700\u6c42\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6587\u4ef6\u548c\u76ee\u5f55\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u67e5\u770b\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>getcwd()<\/code>\u51fd\u6570\u6765\u67e5\u770b\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\u3002\u53ea\u9700\u5728\u60a8\u7684Python\u811a\u672c\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nprint(os.getcwd())\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\u8def\u5f84\u3002<\/p>\n<p><strong>\u600e\u6837\u5728Python\u4e2d\u4e34\u65f6\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55\uff1f<\/strong><br \/>\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u7684<code>chdir()<\/code>\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u4e34\u65f6\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nos.chdir(&#39;\/path\/to\/new\/directory&#39;)\n<\/code><\/pre>\n<p>\u8bf7\u5c06<code>\/path\/to\/new\/directory<\/code>\u66ff\u6362\u4e3a\u60a8\u5e0c\u671b\u8bbe\u7f6e\u7684\u65b0\u76ee\u5f55\u3002\u8fd9\u79cd\u66f4\u6539\u53ea\u5728\u5f53\u524d\u4f1a\u8bdd\u4e2d\u6709\u6548\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u811a\u672c\u4e2d\u6301\u4e45\u6027\u5730\u8bbe\u7f6e\u9ed8\u8ba4\u76ee\u5f55\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u60f3\u5728\u6bcf\u6b21\u8fd0\u884c\u811a\u672c\u65f6\u90fd\u4f7f\u7528\u7279\u5b9a\u76ee\u5f55\uff0c\u53ef\u4ee5\u5728\u811a\u672c\u7684\u5f00\u5934\u4f7f\u7528<code>os.chdir()<\/code>\u6765\u8bbe\u7f6e\u76ee\u5f55\u3002\u786e\u4fdd\u5728\u811a\u672c\u7684\u5176\u4ed6\u90e8\u5206\u4e4b\u524d\u8c03\u7528\u6b64\u51fd\u6570\uff0c\u4ee5\u4fdd\u8bc1\u6240\u6709\u6587\u4ef6\u64cd\u4f5c\u90fd\u5728\u65b0\u7684\u5de5\u4f5c\u76ee\u5f55\u4e2d\u6267\u884c\u3002\u6bd4\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nos.chdir(&#39;\/path\/to\/default\/directory&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u8bbe\u7f6e\u540e\uff0c\u540e\u7eed\u7684\u6587\u4ef6\u64cd\u4f5c\u5c06\u9ed8\u8ba4\u5728\u6307\u5b9a\u7684\u76ee\u5f55\u4e2d\u8fdb\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u6539\u53d8Python\u9ed8\u8ba4\u76ee\u5f55\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528os\u6a21\u5757\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3001\u66f4\u6539Python\u811a\u672c\u7684\u542f\u52a8\u8def\u5f84\u3002 \u5176\u4e2d\uff0c\u4f7f\u7528o [&hellip;]","protected":false},"author":3,"featured_media":1177193,"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\/1177184"}],"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=1177184"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1177184\/revisions"}],"predecessor-version":[{"id":1177194,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1177184\/revisions\/1177194"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1177193"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1177184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1177184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1177184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}