{"id":1023661,"date":"2024-12-30T14:07:03","date_gmt":"2024-12-30T06:07:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1023661.html"},"modified":"2024-12-30T14:07:05","modified_gmt":"2024-12-30T06:07:05","slug":"python%e4%bf%9d%e5%ad%98%e8%b7%af%e5%be%84%e5%a6%82%e4%bd%95%e7%bc%96%e5%86%99-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1023661.html","title":{"rendered":"python\u4fdd\u5b58\u8def\u5f84\u5982\u4f55\u7f16\u5199"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/185b3719-63ed-4e1f-8f23-3cd71e3874b4.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4fdd\u5b58\u8def\u5f84\u5982\u4f55\u7f16\u5199\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u4fdd\u5b58\u8def\u5f84\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4f60\u9700\u8981\u4fdd\u5b58\u4ec0\u4e48\u7c7b\u578b\u7684\u6587\u4ef6\u4ee5\u53ca\u4f60\u5e0c\u671b\u5982\u4f55\u7ec4\u7ec7\u6587\u4ef6\u8def\u5f84\u3002<strong>\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3001\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u3001\u4f7f\u7528os\u6a21\u5757\u7ba1\u7406\u8def\u5f84\u3001\u4f7f\u7528pathlib\u6a21\u5757\u7ba1\u7406\u8def\u5f84<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528os\u6a21\u5757\u7ba1\u7406\u8def\u5f84\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p><p>\u7edd\u5bf9\u8def\u5f84\u662f\u6307\u4ece\u6839\u76ee\u5f55\u5f00\u59cb\u7684\u5b8c\u6574\u8def\u5f84\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u6307\u5b9a\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u6765\u4fdd\u5b58\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;C:\/Users\/YourUsername\/Documents\/example.txt&quot;<\/p>\n<p>with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&quot;This is an example.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u76f4\u89c2\uff0c\u4f46\u4e0d\u591f\u7075\u6d3b\uff0c\u56e0\u4e3a\u5982\u679c\u6587\u4ef6\u7684\u5b58\u50a8\u4f4d\u7f6e\u53d1\u751f\u53d8\u5316\uff0c\u4ee3\u7801\u4e5f\u9700\u8981\u76f8\u5e94\u4fee\u6539\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p><p>\u76f8\u5bf9\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\u3002\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5177\u79fb\u690d\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;example.txt&quot;<\/p>\n<p>with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&quot;This is an example.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6587\u4ef6\u5c06\u88ab\u4fdd\u5b58\u5230\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528os\u6a21\u5757\u7ba1\u7406\u8def\u5f84<\/strong><\/h2>\n<p><p>os\u6a21\u5757\u63d0\u4f9b\u4e86\u5904\u7406\u8def\u5f84\u7684\u8bb8\u591a\u51fd\u6570\uff0c\u4f7f\u5f97\u5728\u8de8\u5e73\u53f0\u7f16\u7a0b\u4e2d\u7ba1\u7406\u8def\u5f84\u66f4\u52a0\u65b9\u4fbf\u3002\u4f8b\u5982\uff0c\u4f7f\u7528os.path.join()\u53ef\u4ee5\u751f\u6210\u9002\u5408\u5f53\u524d\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>directory = &quot;Documents&quot;<\/p>\n<p>file_name = &quot;example.txt&quot;<\/p>\n<p>file_path = os.path.join(directory, file_name)<\/p>\n<h2><strong>\u786e\u4fdd\u76ee\u5f55\u5b58\u5728<\/strong><\/h2>\n<p>if not os.path.exists(directory):<\/p>\n<p>    os.makedirs(directory)<\/p>\n<p>with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&quot;This is an example.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528pathlib\u6a21\u5757\u7ba1\u7406\u8def\u5f84<\/strong><\/h2>\n<p><p>Python 3.4\u5f15\u5165\u4e86pathlib\u6a21\u5757\uff0c\u7528\u4e8e\u9762\u5411\u5bf9\u8c61\u5730\u7ba1\u7406\u8def\u5f84\u3002pathlib\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c42\u6b21\u7684\u63a5\u53e3\uff0c\u4f7f\u5f97\u8def\u5f84\u64cd\u4f5c\u66f4\u52a0\u7b80\u5355\u548c\u76f4\u89c2\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>directory = Path(&quot;Documents&quot;)<\/p>\n<p>file_path = directory \/ &quot;example.txt&quot;<\/p>\n<h2><strong>\u786e\u4fdd\u76ee\u5f55\u5b58\u5728<\/strong><\/h2>\n<p>directory.mkdir(parents=True, exist_ok=True)<\/p>\n<p>with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&quot;This is an example.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u7684Path\u5bf9\u8c61\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u8def\u5f84\u62fc\u63a5\u3001\u521b\u5efa\u76ee\u5f55\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u4f7f\u7528os\u6a21\u5757\u7ba1\u7406\u8def\u5f84<\/strong><\/h2>\n<p><p>\u4f7f\u7528os\u6a21\u5757\u7ba1\u7406\u8def\u5f84\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u5728\u5904\u7406\u8de8\u5e73\u53f0\u8def\u5f84\u65f6\u975e\u5e38\u6709\u6548\u3002os\u6a21\u5757\u4e2d\u7684os.path\u5b50\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6709\u7528\u7684\u51fd\u6570\u6765\u5904\u7406\u8def\u5f84\uff0c\u4f8b\u5982os.path.join()\u3001os.path.exists()\u548cos.makedirs()\u7b49\u3002<\/p>\n<\/p>\n<p><h2>1. os.path.join()<\/h2>\n<\/p>\n<p><p>os.path.join()\u51fd\u6570\u53ef\u4ee5\u5c06\u591a\u4e2a\u8def\u5f84\u7ec4\u4ef6\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u4e00\u4e2a\u9002\u5408\u5f53\u524d\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>directory = &quot;Documents&quot;<\/p>\n<p>file_name = &quot;example.txt&quot;<\/p>\n<p>file_path = os.path.join(directory, file_name)<\/p>\n<p>print(file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Windows\u4e0a\uff0c\u8f93\u51fa\u5c06\u662f&quot;Documents\\example.txt&quot;\uff0c\u800c\u5728Unix\/Linux\u4e0a\uff0c\u8f93\u51fa\u5c06\u662f&quot;Documents\/example.txt&quot;\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u624b\u52a8\u62fc\u63a5\u8def\u5f84\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h2>2. os.path.exists()<\/h2>\n<\/p>\n<p><p>os.path.exists()\u51fd\u6570\u7528\u4e8e\u68c0\u67e5\u6307\u5b9a\u8def\u5f84\u662f\u5426\u5b58\u5728\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &quot;Documents\/example.txt&quot;<\/p>\n<p>if os.path.exists(file_path):<\/p>\n<p>    print(&quot;The file exists.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The file does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u6267\u884c\u6587\u4ef6\u64cd\u4f5c\u4e4b\u524d\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u907f\u514d\u51fa\u73b0\u6587\u4ef6\u4e0d\u5b58\u5728\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h2>3. os.makedirs()<\/h2>\n<\/p>\n<p><p>os.makedirs()\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u591a\u7ea7\u76ee\u5f55\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>directory = &quot;Documents\/2023\/October&quot;<\/p>\n<p>if not os.path.exists(directory):<\/p>\n<p>    os.makedirs(directory)<\/p>\n<p>    print(&quot;The directory was created.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The directory already exists.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u76ee\u5f55\u5b58\u5728\uff0c\u5728\u4fdd\u5b58\u6587\u4ef6\u4e4b\u524d\u521b\u5efa\u5fc5\u8981\u7684\u76ee\u5f55\u7ed3\u6784\u3002<\/p>\n<\/p>\n<h2><strong>\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\u4fdd\u5b58\u8def\u5f84\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3001\u76f8\u5bf9\u8def\u5f84\u3001os\u6a21\u5757\u6216pathlib\u6a21\u5757\u3002<strong>\u4f7f\u7528os\u6a21\u5757\u7ba1\u7406\u8def\u5f84<\/strong>\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u5728\u5904\u7406\u8de8\u5e73\u53f0\u8def\u5f84\u65f6\u975e\u5e38\u6709\u6548\u3002\u901a\u8fc7os.path.join()\u751f\u6210\u8def\u5f84\u3001os.path.exists()\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\u3001os.makedirs()\u521b\u5efa\u76ee\u5f55\u7b49\u51fd\u6570\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002pathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u7ba1\u7406\u63a5\u53e3\uff0c\u4f7f\u5f97\u8def\u5f84\u64cd\u4f5c\u66f4\u52a0\u7b80\u5355\u548c\u76f4\u89c2\u3002\u9009\u62e9\u9002\u5408\u7684\u8def\u5f84\u7ba1\u7406\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u5065\u58ee\u548c\u7075\u6d3b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6307\u5b9a\u6587\u4ef6\u4fdd\u5b58\u8def\u5f84\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a\u5b8c\u6574\u8def\u5f84\u6765\u8bbe\u7f6e\u6587\u4ef6\u7684\u4fdd\u5b58\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u5c06\u8def\u5f84\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002\u786e\u4fdd\u8def\u5f84\u5b58\u5728\u5e76\u4e14\u6709\u5199\u5165\u6743\u9650\uff0c\u624d\u80fd\u987a\u5229\u4fdd\u5b58\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;C:\/Users\/Username\/Documents\/example.txt&#39;, &#39;w&#39;) as file:\n    file.write(&#39;Hello, World!&#39;)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84\uff1f<\/strong><br \/>\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\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>getcwd()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u4fbf\u4e8e\u7406\u89e3\u76f8\u5bf9\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import os\ncurrent_directory = os.getcwd()\nprint(current_directory)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u7ed3\u5408\u8fd9\u4e24\u79cd\u8def\u5f84\uff0c\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u7ba1\u7406\u6587\u4ef6\u7684\u4fdd\u5b58\u548c\u8bfb\u53d6\u3002<\/p>\n<p><strong>\u5982\u679c\u6307\u5b9a\u7684\u4fdd\u5b58\u8def\u5f84\u4e0d\u5b58\u5728\uff0cPython\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5982\u679c\u5c1d\u8bd5\u5728\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u8def\u5f84\u4e2d\u4fdd\u5b58\u6587\u4ef6\uff0cPython\u5c06\u5f15\u53d1<code>FileNotFoundError<\/code>\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>makedirs()<\/code>\u65b9\u6cd5\u63d0\u524d\u521b\u5efa\u6240\u9700\u7684\u76ee\u5f55\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import os\nsave_path = &#39;C:\/Users\/Username\/Documents\/NewFolder&#39;\nos.makedirs(save_path, exist_ok=True)  # \u5982\u679c\u76ee\u5f55\u5df2\u5b58\u5728\u4e0d\u4f1a\u62a5\u9519\nwith open(os.path.join(save_path, &#39;example.txt&#39;), &#39;w&#39;) as file:\n    file.write(&#39;Hello, World!&#39;)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u4fdd\u5b58\u6587\u4ef6\u4e4b\u524d\u76ee\u5f55\u5df2\u521b\u5efa\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514d\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4fdd\u5b58\u8def\u5f84\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4f60\u9700\u8981\u4fdd\u5b58\u4ec0\u4e48\u7c7b\u578b\u7684\u6587\u4ef6\u4ee5\u53ca\u4f60\u5e0c\u671b\u5982\u4f55\u7ec4\u7ec7\u6587\u4ef6\u8def\u5f84\u3002\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1023669,"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\/1023661"}],"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=1023661"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1023661\/revisions"}],"predecessor-version":[{"id":1023670,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1023661\/revisions\/1023670"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1023669"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1023661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1023661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1023661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}