{"id":941481,"date":"2024-12-26T21:34:28","date_gmt":"2024-12-26T13:34:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/941481.html"},"modified":"2024-12-26T21:34:30","modified_gmt":"2024-12-26T13:34:30","slug":"python%e5%a6%82%e4%bd%95%e6%8d%a2%e7%9b%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/941481.html","title":{"rendered":"python\u5982\u4f55\u6362\u76d8"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075708\/19b2efae-2771-4d71-aec4-cbce0a588296.webp\" alt=\"python\u5982\u4f55\u6362\u76d8\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u6362\u76d8\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3001\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u6a21\u5757\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3002<\/strong>\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u6a21\u5757<\/strong>\u662f\u6700\u5e38\u7528\u4e14\u4fbf\u6377\u7684\u65b9\u6cd5\u3002\u901a\u8fc7os\u6a21\u5757\u7684\u51fd\u6570\uff0cPython\u53ef\u4ee5\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\uff0c\u4ece\u800c\u5b9e\u73b0\u78c1\u76d8\u7684\u5207\u6362\u548c\u64cd\u4f5c\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u53ef\u4ee5\u901a\u8fc7os.chdir()\u65b9\u6cd5\u5207\u6362\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u5230\u6307\u5b9a\u78c1\u76d8\u8def\u5f84\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u4e4b\u4e00\u5c31\u662f\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6765\u8bbf\u95ee\u4e0d\u540c\u78c1\u76d8\u4e0a\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u3002\u7edd\u5bf9\u8def\u5f84\u662f\u6307\u6587\u4ef6\u6216\u76ee\u5f55\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u5b8c\u6574\u8def\u5f84\uff0c\u5b83\u4e0d\u4f9d\u8d56\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002<\/p>\n<\/p>\n<ol>\n<li>\u7edd\u5bf9\u8def\u5f84\u7684\u4f18\u52bf<\/li>\n<\/ol>\n<p><p>\u7edd\u5bf9\u8def\u5f84\u53ef\u4ee5\u907f\u514d\u56e0\u4e3a\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u4e0d\u540c\u800c\u5bfc\u81f4\u7684\u8def\u5f84\u9519\u8bef\u3002\u5bf9\u4e8e\u9700\u8981\u8de8\u78c1\u76d8\u8bbf\u95ee\u6587\u4ef6\u7684\u573a\u666f\uff0c\u7edd\u5bf9\u8def\u5f84\u63d0\u4f9b\u4e86\u4e00\u4e2a\u76f4\u63a5\u800c\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u65f6\uff0c\u53ea\u9700\u786e\u4fdd\u8def\u5f84\u7684\u6b63\u786e\u6027\uff0c\u5373\u53ef\u5728\u4e0d\u540c\u78c1\u76d8\u4e4b\u95f4\u8bbf\u95ee\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u793a\u4f8b\u4ee3\u7801<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u6253\u5f00D\u76d8\u4e2d\u7684\u6587\u4ef6<\/p>\n<p>file_path = &quot;D:\\\\example\\\\file.txt&quot;<\/p>\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\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u4f7f\u7528\u4e86D\u76d8\u7684\u7edd\u5bf9\u8def\u5f84\u6765\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u5728\u8def\u5f84\u53d8\u66f4\u6216\u8de8\u5e73\u53f0\u65f6\u9700\u8981\u624b\u52a8\u8c03\u6574\u8def\u5f84\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u7684os\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u529f\u80fd\uff0c\u5176\u4e2d\u5305\u62ec\u6539\u53d8\u5de5\u4f5c\u76ee\u5f55\u548c\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>os.chdir()\u65b9\u6cd5<\/li>\n<\/ol>\n<p><p>os.chdir()\u662fPython\u4e2d\u7528\u4e8e\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u51fd\u6570\u3002\u901a\u8fc7\u8be5\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u5207\u6362\u5230\u6307\u5b9a\u7684\u78c1\u76d8\u8def\u5f84\uff0c\u4ece\u800c\u5b9e\u73b0\u201c\u6362\u76d8\u201d\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5207\u6362\u5230D\u76d8<\/strong><\/h2>\n<p>os.chdir(&quot;D:\\\\&quot;)<\/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<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528os.chdir()\u65b9\u6cd5\u5c06\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u5207\u6362\u5230D\u76d8\uff0c\u5e76\u4f7f\u7528os.getcwd()\u9a8c\u8bc1\u5f53\u524d\u76ee\u5f55\u7684\u53d8\u66f4\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>os.path\u6a21\u5757\u7684\u529f\u80fd<\/li>\n<\/ol>\n<p><p>os.path\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u8def\u5f84\u76f8\u5173\u7684\u64cd\u4f5c\uff0c\u5982\u62fc\u63a5\u8def\u5f84\u3001\u5224\u65ad\u8def\u5f84\u5b58\u5728\u6027\u7b49\u3002\u7ed3\u5408os\u6a21\u5757\uff0c\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5904\u7406\u8def\u5f84\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u62fc\u63a5\u8def\u5f84<\/strong><\/h2>\n<p>file_path = os.path.join(&quot;D:\\\\&quot;, &quot;example&quot;, &quot;file.txt&quot;)<\/p>\n<h2><strong>\u5224\u65ad\u8def\u5f84\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if os.path.exists(file_path):<\/p>\n<p>    print(&quot;\u6587\u4ef6\u5b58\u5728:&quot;, file_path)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u6587\u4ef6\u4e0d\u5b58\u5728:&quot;, file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7os.path.join()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u907f\u514d\u624b\u52a8\u5904\u7406\u8def\u5f84\u5206\u9694\u7b26\u7684\u95ee\u9898\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5728Python\u811a\u672c\u4e2d\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u547d\u4ee4\u884c\u5de5\u5177\u6765\u5b9e\u73b0\u6362\u76d8\u64cd\u4f5c\u3002\u901a\u8fc7subprocess\u6a21\u5757\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u3002<\/p>\n<\/p>\n<ol>\n<li>subprocess\u6a21\u5757<\/li>\n<\/ol>\n<p><p>subprocess\u6a21\u5757\u7528\u4e8e\u542f\u52a8\u5b50\u8fdb\u7a0b\u5e76\u4e0e\u5176\u8fdb\u884c\u901a\u4fe1\uff0c\u662fPython\u4e2d\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u7684\u6807\u51c6\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u4f7f\u7528\u547d\u4ee4\u884c\u5207\u6362\u5230D\u76d8<\/strong><\/h2>\n<p>subprocess.run([&quot;cmd&quot;, &quot;\/c&quot;, &quot;D:&quot;])<\/p>\n<h2><strong>\u5207\u6362\u6210\u529f\u540e\uff0c\u53ef\u4ee5\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u547d\u4ee4<\/strong><\/h2>\n<p>subprocess.run([&quot;cmd&quot;, &quot;\/c&quot;, &quot;dir&quot;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528subprocess.run()\u65b9\u6cd5\u6267\u884c\u4e86Windows\u547d\u4ee4\u884c\u7684\u6362\u76d8\u64cd\u4f5c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u9700\u8981\u8c03\u6574\u547d\u4ee4\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u8de8\u5e73\u53f0\u8003\u8651<\/li>\n<\/ol>\n<p><p>\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u6362\u76d8\u64cd\u4f5c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u547d\u4ee4\u3002\u4f8b\u5982\uff0c\u5728Linux\u6216MacOS\u4e0a\uff0c\u6362\u76d8\u901a\u5e38\u6d89\u53ca\u6302\u8f7d\u548c\u5378\u8f7d\u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u4f7f\u7528subprocess\u6a21\u5757\u65f6\u9700\u8981\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u5224\u65ad\u6267\u884c\u76f8\u5e94\u7684\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86Python\u4e2d\u5b9e\u73b0\u6362\u76d8\u7684\u51e0\u79cd\u5e38\u7528\u65b9\u6cd5\u3002<strong>\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84<\/strong>\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u8def\u5f84\u56fa\u5b9a\u7684\u60c5\u51b5\uff1b<strong>\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u6a21\u5757<\/strong>\u662f\u6700\u7075\u6d3b\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u80fd\u591f\u901a\u8fc7Python\u4ee3\u7801\u76f4\u63a5\u64cd\u4f5c\u6587\u4ef6\u7cfb\u7edf\uff1b<strong>\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177<\/strong>\u9002\u7528\u4e8e\u9700\u8981\u7ed3\u5408\u64cd\u4f5c\u7cfb\u7edf\u547d\u4ee4\u7684\u590d\u6742\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u73af\u5883\u3002\u4f8b\u5982\uff0c\u5728\u9700\u8981\u8de8\u5e73\u53f0\u517c\u5bb9\u65f6\uff0c\u4f18\u5148\u8003\u8651\u4f7f\u7528os\u6a21\u5757\u7684\u529f\u80fd\uff1b\u5728\u9700\u8981\u6267\u884c\u590d\u6742\u7684\u7cfb\u7edf\u547d\u4ee4\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u7ed3\u5408subprocess\u6a21\u5757\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u786e\u4fdd\u8def\u5f84\u7684\u6b63\u786e\u6027\u548c\u64cd\u4f5c\u7684\u5b89\u5168\u6027\u59cb\u7ec8\u662f\u6700\u4e3a\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5207\u6362\u5de5\u4f5c\u76ee\u5f55\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5207\u6362\u5de5\u4f5c\u76ee\u5f55\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>chdir()<\/code>\u51fd\u6570\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165<code>os<\/code>\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>os.chdir(&#39;\u76ee\u6807\u76ee\u5f55\u8def\u5f84&#39;)<\/code>\u6765\u66f4\u6539\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u786e\u4fdd\u63d0\u4f9b\u7684\u8def\u5f84\u662f\u6709\u6548\u7684\uff0c\u5e76\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6216\u76f8\u5bf9\u8def\u5f84\u5747\u53ef\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u68c0\u67e5\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff1f<\/strong><br \/>\u8981\u67e5\u770b\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>getcwd()<\/code>\u51fd\u6570\u3002\u53ea\u9700\u5bfc\u5165<code>os<\/code>\u6a21\u5757\u5e76\u8c03\u7528<code>os.getcwd()<\/code>\uff0c\u5b83\u5c06\u8fd4\u56de\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\u3002\u8fd9\u5bf9\u4e8e\u786e\u8ba4\u60a8\u662f\u5426\u6210\u529f\u5207\u6362\u5230\u76ee\u6807\u76ee\u5f55\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n<p><strong>\u5982\u679c\u60f3\u5728Python\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684\u78c1\u76d8\u9a71\u52a8\u5668\uff0c\u8be5\u600e\u4e48\u505a\uff1f<\/strong><br \/>\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u5982\u679c\u60a8\u60f3\u5728Python\u4e2d\u5207\u6362\u5230\u4e0d\u540c\u7684\u78c1\u76d8\u9a71\u52a8\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.chdir()<\/code>\u51fd\u6570\u5e76\u63d0\u4f9b\u78c1\u76d8\u9a71\u52a8\u5668\u7684\u8def\u5f84\uff0c\u4f8b\u5982<code>os.chdir(&#39;D:\\\\&#39;)<\/code>\u3002\u5728Unix\/Linux\u7cfb\u7edf\u4e2d\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u76f8\u5e94\u7684\u8def\u5f84\u6765\u8bbf\u95ee\u5176\u4ed6\u6302\u8f7d\u7684\u9a71\u52a8\u5668\u6216\u76ee\u5f55\u3002\u786e\u4fdd\u8def\u5f84\u683c\u5f0f\u6b63\u786e\uff0c\u4ee5\u907f\u514d\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u6362\u76d8\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3001\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u6a21\u5757\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":941485,"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\/941481"}],"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=941481"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/941481\/revisions"}],"predecessor-version":[{"id":941487,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/941481\/revisions\/941487"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/941485"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=941481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=941481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=941481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}