{"id":1009027,"date":"2024-12-27T11:08:01","date_gmt":"2024-12-27T03:08:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1009027.html"},"modified":"2024-12-27T11:08:05","modified_gmt":"2024-12-27T03:08:05","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%9b%b4%e6%94%b9%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1009027.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u66f4\u6539\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084112\/6ad87c19-0131-45c0-b49d-c3ddb70a4c4e.webp\" alt=\"python\u4e2d\u5982\u4f55\u66f4\u6539\u8def\u5f84\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u66f4\u6539\u8def\u5f84\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>chdir()<\/code>\u51fd\u6570\u3001\u901a\u8fc7<code>sys<\/code>\u6a21\u5757\u8c03\u6574\u8def\u5f84\u3001\u4ee5\u53ca\u5728\u811a\u672c\u4e2d\u76f4\u63a5\u8bbe\u7f6e\u8def\u5f84\u7b49\u3002<strong>\u4f7f\u7528<code>os.chdir()<\/code>\u51fd\u6570\u3001\u901a\u8fc7<code>sys.path<\/code>\u8c03\u6574\u3001\u5229\u7528<code>pathlib<\/code>\u6a21\u5757<\/strong>\u662f\u5b9e\u73b0\u8def\u5f84\u66f4\u6539\u7684\u4e3b\u8981\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528<code>os.chdir()<\/code>\u51fd\u6570\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><code>os.chdir()<\/code>\u662fPython\u6807\u51c6\u5e93\u4e2d<code>os<\/code>\u6a21\u5757\u63d0\u4f9b\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u66f4\u6539\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\u3002\u4f7f\u7528\u65f6\u53ea\u9700\u4f20\u5165\u76ee\u6807\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\uff0c\u5c31\u53ef\u4ee5\u5c06\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\u66f4\u6539\u4e3a\u76ee\u6807\u8def\u5f84\u3002\u8fd9\u5728\u9700\u8981\u5728\u7a0b\u5e8f\u4e2d\u52a8\u6001\u8c03\u6574\u6587\u4ef6\u8bbf\u95ee\u8def\u5f84\u65f6\u975e\u5e38\u6709\u7528\u3002<\/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_directory = os.getcwd()<\/p>\n<p>print(f&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55: {current_directory}&quot;)<\/p>\n<h2><strong>\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>os.chdir(&#39;\/path\/to\/new\/directory&#39;)<\/p>\n<h2><strong>\u9a8c\u8bc1\u66f4\u6539\u540e\u7684\u76ee\u5f55<\/strong><\/h2>\n<p>new_directory = os.getcwd()<\/p>\n<p>print(f&quot;\u66f4\u6539\u540e\u7684\u5de5\u4f5c\u76ee\u5f55: {new_directory}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u63a2\u8ba8\u5176\u4ed6\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001OS\u6a21\u5757\u7684\u4f7f\u7528<\/p>\n<\/p>\n<ol>\n<li><strong>os\u6a21\u5757\u7684\u57fa\u672c\u4ecb\u7ecd<\/strong><\/li>\n<\/ol>\n<p><p><code>os<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u4fbf\u6377\u7684\u65b9\u5f0f\u6765\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u3002\u4f7f\u7528<code>os<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u6267\u884c\u64cd\u4f5c\u7cfb\u7edf\u76f8\u5173\u7684\u529f\u80fd\uff0c\u5982\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u64cd\u4f5c\u3001\u73af\u5883\u53d8\u91cf\u7684\u83b7\u53d6\u548c\u8bbe\u7f6e\u3001\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u7b49\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528os.chdir()\u66f4\u6539\u76ee\u5f55<\/strong><\/li>\n<\/ol>\n<p><p><code>os.chdir()<\/code>\u51fd\u6570\u662f<code>os<\/code>\u6a21\u5757\u4e2d\u4e13\u95e8\u7528\u4e8e\u66f4\u6539\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u51fd\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<code>os.chdir()<\/code>\u7684\u793a\u4f8b\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_directory = os.getcwd()<\/p>\n<p>print(f&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55: {current_directory}&quot;)<\/p>\n<h2><strong>\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>os.chdir(&#39;\/path\/to\/new\/directory&#39;)<\/p>\n<h2><strong>\u9a8c\u8bc1\u66f4\u6539\u540e\u7684\u76ee\u5f55<\/strong><\/h2>\n<p>new_directory = os.getcwd()<\/p>\n<p>print(f&quot;\u66f4\u6539\u540e\u7684\u5de5\u4f5c\u76ee\u5f55: {new_directory}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>os.getcwd()<\/code>\u7528\u4e8e\u83b7\u53d6\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\uff0c\u800c<code>os.chdir()<\/code>\u7528\u4e8e\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55\u3002\u66f4\u6539\u540e\uff0c\u53ef\u4ee5\u518d\u6b21\u8c03\u7528<code>os.getcwd()<\/code>\u6765\u9a8c\u8bc1\u76ee\u5f55\u662f\u5426\u5df2\u66f4\u6539\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>os\u6a21\u5757\u7684\u5176\u4ed6\u529f\u80fd<\/strong><\/li>\n<\/ol>\n<p><p>\u9664\u4e86\u66f4\u6539\u76ee\u5f55\uff0c<code>os<\/code>\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u6709\u7528\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u521b\u5efa\u76ee\u5f55\uff1a<code>os.mkdir(&#39;\/path\/to\/directory&#39;)<\/code><\/li>\n<li>\u5220\u9664\u76ee\u5f55\uff1a<code>os.rmdir(&#39;\/path\/to\/directory&#39;)<\/code><\/li>\n<li>\u5217\u51fa\u76ee\u5f55\u5185\u5bb9\uff1a<code>os.listdir(&#39;\/path\/to\/directory&#39;)<\/code><\/li>\n<li>\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\uff1a<code>os.path.exists(&#39;\/path\/to\/directory&#39;)<\/code><\/li>\n<\/ul>\n<p><p>\u8fd9\u4e9b\u529f\u80fd\u4f7f\u5f97<code>os<\/code>\u6a21\u5757\u6210\u4e3a\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u7684\u5f3a\u5927\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001SYS\u6a21\u5757\u7684\u4f7f\u7528<\/p>\n<\/p>\n<ol>\n<li><strong>sys\u6a21\u5757\u7684\u57fa\u672c\u4ecb\u7ecd<\/strong><\/li>\n<\/ol>\n<p><p><code>sys<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9b\u4e0ePython\u89e3\u91ca\u5668\u76f8\u5173\u7684\u529f\u80fd\u3002\u901a\u8fc7<code>sys<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u8bbf\u95ee\u548c\u4fee\u6539\u4e0ePython\u89e3\u91ca\u5668\u76f8\u5173\u7684\u53c2\u6570\u548c\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528sys.path\u66f4\u6539\u6a21\u5757\u641c\u7d22\u8def\u5f84<\/strong><\/li>\n<\/ol>\n<p><p><code>sys.path<\/code>\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u4e86Python\u89e3\u91ca\u5668\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u3002\u5728\u9700\u8981\u5bfc\u5165\u4e0d\u5728\u9ed8\u8ba4\u641c\u7d22\u8def\u5f84\u4e2d\u7684\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539<code>sys.path<\/code>\u6765\u6dfb\u52a0\u65b0\u7684\u641c\u7d22\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u67e5\u770b\u5f53\u524d\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84<\/strong><\/h2>\n<p>print(&quot;\u5f53\u524d\u6a21\u5757\u641c\u7d22\u8def\u5f84:&quot;)<\/p>\n<p>for path in sys.path:<\/p>\n<p>    print(path)<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u641c\u7d22\u8def\u5f84<\/strong><\/h2>\n<p>sys.path.append(&#39;\/path\/to\/new\/module\/directory&#39;)<\/p>\n<h2><strong>\u9a8c\u8bc1\u66f4\u6539\u540e\u7684\u641c\u7d22\u8def\u5f84<\/strong><\/h2>\n<p>print(&quot;\\n\u66f4\u6539\u540e\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84:&quot;)<\/p>\n<p>for path in sys.path:<\/p>\n<p>    print(path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>sys\u6a21\u5757\u7684\u5176\u4ed6\u529f\u80fd<\/strong><\/li>\n<\/ol>\n<p><p>\u9664\u4e86\u4fee\u6539\u6a21\u5757\u641c\u7d22\u8def\u5f84\uff0c<code>sys<\/code>\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u529f\u80fd\uff0c\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570\uff1a<code>sys.argv<\/code><\/li>\n<li>\u9000\u51fa\u7a0b\u5e8f\uff1a<code>sys.exit()<\/code><\/li>\n<li>\u83b7\u53d6Python\u7248\u672c\uff1a<code>sys.version<\/code><\/li>\n<\/ul>\n<p><p>\u8fd9\u4e9b\u529f\u80fd\u4f7f\u5f97<code>sys<\/code>\u6a21\u5757\u5728\u5904\u7406\u4e0ePython\u89e3\u91ca\u5668\u76f8\u5173\u7684\u4efb\u52a1\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001PATHLIB\u6a21\u5757\u7684\u4f7f\u7528<\/p>\n<\/p>\n<ol>\n<li><strong>pathlib\u6a21\u5757\u7684\u57fa\u672c\u4ecb\u7ecd<\/strong><\/li>\n<\/ol>\n<p><p><code>pathlib<\/code>\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3002\u4e0e\u4f20\u7edf\u7684\u5b57\u7b26\u4e32\u5904\u7406\u8def\u5f84\u4e0d\u540c\uff0c<code>pathlib<\/code>\u63d0\u4f9b\u4e86\u66f4\u76f4\u89c2\u548c\u4fbf\u6377\u7684\u8def\u5f84\u64cd\u4f5c\u63a5\u53e3\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528pathlib\u66f4\u6539\u76ee\u5f55<\/strong><\/li>\n<\/ol>\n<p><p>\u867d\u7136<code>pathlib<\/code>\u672c\u8eab\u6ca1\u6709\u76f4\u63a5\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55\u7684\u529f\u80fd\uff0c\u4f46\u53ef\u4ee5\u7ed3\u5408<code>os<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>import os<\/p>\n<h2><strong>\u4f7f\u7528pathlib\u521b\u5efa\u8def\u5f84\u5bf9\u8c61<\/strong><\/h2>\n<p>new_directory = Path(&#39;\/path\/to\/new\/directory&#39;)<\/p>\n<h2><strong>\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>os.chdir(new_directory)<\/p>\n<h2><strong>\u9a8c\u8bc1\u66f4\u6539\u540e\u7684\u76ee\u5f55<\/strong><\/h2>\n<p>current_directory = os.getcwd()<\/p>\n<p>print(f&quot;\u66f4\u6539\u540e\u7684\u5de5\u4f5c\u76ee\u5f55: {current_directory}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>Path<\/code>\u7c7b\u521b\u5efa\u4e86\u4e00\u4e2a\u8def\u5f84\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>os.chdir()<\/code>\u51fd\u6570\u66f4\u6539\u4e86\u5de5\u4f5c\u76ee\u5f55\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>pathlib\u6a21\u5757\u7684\u5176\u4ed6\u529f\u80fd<\/strong><\/li>\n<\/ol>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u6709\u7528\u7684\u529f\u80fd\uff0c\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u521b\u5efa\u76ee\u5f55\uff1a<code>Path(&#39;\/path\/to\/directory&#39;).mkdir()<\/code><\/li>\n<li>\u5220\u9664\u6587\u4ef6\uff1a<code>Path(&#39;\/path\/to\/file&#39;).unlink()<\/code><\/li>\n<li>\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\uff1a<code>Path(&#39;\/path\/to\/directory&#39;).exists()<\/code><\/li>\n<\/ul>\n<p><p>\u8fd9\u4e9b\u529f\u80fd\u4f7f\u5f97<code>pathlib<\/code>\u6a21\u5757\u6210\u4e3a\u5904\u7406\u8def\u5f84\u64cd\u4f5c\u7684\u73b0\u4ee3\u5316\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001PYTHON\u811a\u672c\u4e2d\u7684\u8def\u5f84\u8bbe\u7f6e<\/p>\n<\/p>\n<ol>\n<li><strong>\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Python\u811a\u672c\u4e2d\u4f7f\u7528\u8def\u5f84\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u6216\u7edd\u5bf9\u8def\u5f84\u3002\u76f8\u5bf9\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\uff0c\u800c\u7edd\u5bf9\u8def\u5f84\u662f\u4ece\u6839\u76ee\u5f55\u5f00\u59cb\u7684\u5b8c\u6574\u8def\u5f84\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5728\u811a\u672c\u4e2d\u8bbe\u7f6e\u8def\u5f84<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u7f16\u5199Python\u811a\u672c\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8bbe\u7f6e\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = os.path.join(os.getcwd(), &#39;data&#39;, &#39;file.txt&#39;)<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6<\/strong><\/h2>\n<p>with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>os.path.join()<\/code>\u51fd\u6570\u6784\u5efa\u4e86\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\uff0c\u5e76\u5728\u811a\u672c\u4e2d\u4f7f\u7528\u5b83\u6765\u6253\u5f00\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u8def\u5f84\u8bbe\u7f6e\u7684\u6700\u4f73\u5b9e\u8df5<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u811a\u672c\u4e2d\u4f7f\u7528\u8def\u5f84\u65f6\uff0c\u6700\u597d\u4f7f\u7528<code>os.path.join()<\/code>\u6216<code>pathlib<\/code>\u63d0\u4f9b\u7684\u65b9\u6cd5\u6765\u6784\u5efa\u8def\u5f84\uff0c\u4ee5\u786e\u4fdd\u8de8\u5e73\u53f0\u7684\u517c\u5bb9\u6027\u3002\u6b64\u5916\uff0c\u5c3d\u91cf\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\uff0c\u4ee5\u4fbf\u811a\u672c\u80fd\u591f\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6848<\/p>\n<\/p>\n<ol>\n<li><strong>\u6743\u9650\u95ee\u9898<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u66f4\u6539\u76ee\u5f55\u6216\u8bbf\u95ee\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u95ee\u9898\u3002\u8fd9\u901a\u5e38\u662f\u7531\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u9650\u5236\u4e86\u5bf9\u67d0\u4e9b\u76ee\u5f55\u6216\u6587\u4ef6\u7684\u8bbf\u95ee\u6743\u9650\u3002\u89e3\u51b3\u6b64\u95ee\u9898\u7684\u65b9\u6cd5\u662f\u786e\u4fdd\u811a\u672c\u4ee5\u9002\u5f53\u7684\u6743\u9650\u8fd0\u884c\uff0c\u6216\u8005\u5c06\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u6743\u9650\u8bbe\u7f6e\u4e3a\u53ef\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8def\u5f84\u4e0d\u5b58\u5728<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5c1d\u8bd5\u66f4\u6539\u5230\u4e0d\u5b58\u5728\u7684\u8def\u5f84\u65f6\uff0c\u4f1a\u5f15\u53d1<code>FileNotFoundError<\/code>\u9519\u8bef\u3002\u786e\u4fdd\u5728\u66f4\u6539\u8def\u5f84\u4e4b\u524d\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.exists()<\/code>\u6216<code>Path.exists()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u8de8\u5e73\u53f0\u517c\u5bb9\u6027<\/strong><\/li>\n<\/ol>\n<p><p>\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u8868\u793a\u65b9\u5f0f\u53ef\u80fd\u4e0d\u540c\u3002\u5728\u7f16\u5199\u8de8\u5e73\u53f0\u811a\u672c\u65f6\uff0c\u4f7f\u7528<code>os.path.join()<\/code>\u6216<code>pathlib<\/code>\u6a21\u5757\u6765\u6784\u5efa\u8def\u5f84\uff0c\u4ee5\u786e\u4fdd\u8def\u5f84\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u7684\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e86\u89e3\u548c\u5e94\u7528\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u66f4\u6539\u548c\u7ba1\u7406\u8def\u5f84\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u7f16\u7a0b\u9700\u6c42\u3002\u65e0\u8bba\u662f\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u3001<code>sys<\/code>\u6a21\u5757\u8fd8\u662f<code>pathlib<\/code>\u6a21\u5757\uff0c\u5b83\u4eec\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u8def\u5f84\u64cd\u4f5c\u529f\u80fd\uff0c\u4f7f\u5f97Python\u6210\u4e3a\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u7684\u5f97\u529b\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>getcwd()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\ncurrent_directory = os.getcwd()\nprint(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_directory)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u67e5\u770b\u5f53\u524d\u7684\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u7684<code>chdir()<\/code>\u51fd\u6570\u6765\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55\u662f\u6700\u4f73\u5b9e\u8df5\u3002\u786e\u4fdd\u5728\u66f4\u6539\u4e4b\u524d\u68c0\u67e5\u8981\u66f4\u6539\u7684\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u4ee5\u907f\u514d\u9519\u8bef\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nnew_directory = &#39;\/path\/to\/new\/directory&#39;\nif os.path.exists(new_directory):\n    os.chdir(new_directory)\n    print(&quot;\u5de5\u4f5c\u76ee\u5f55\u5df2\u66f4\u6539\u4e3a:&quot;, os.getcwd())\nelse:\n    print(&quot;\u6307\u5b9a\u7684\u8def\u5f84\u4e0d\u5b58\u5728\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u8def\u5f84\u7684\u6709\u6548\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6587\u4ef6\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684<code>abspath()<\/code>\u51fd\u6570\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nrelative_path = &#39;some\/relative\/path&#39;\nabsolute_path = os.path.abspath(relative_path)\nprint(&quot;\u7edd\u5bf9\u8def\u5f84\u4e3a:&quot;, absolute_path)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u6587\u4ef6\u64cd\u4f5c\u65f6\u4f7f\u7528\u7684\u662f\u6b63\u786e\u7684\u8def\u5f84\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u66f4\u6539\u8def\u5f84\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u4e2d\u7684chdir()\u51fd\u6570\u3001\u901a\u8fc7sys\u6a21\u5757\u8c03\u6574\u8def\u5f84 [&hellip;]","protected":false},"author":3,"featured_media":1009037,"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\/1009027"}],"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=1009027"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1009027\/revisions"}],"predecessor-version":[{"id":1009043,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1009027\/revisions\/1009043"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1009037"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1009027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1009027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1009027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}