{"id":1059630,"date":"2024-12-31T15:26:52","date_gmt":"2024-12-31T07:26:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1059630.html"},"modified":"2024-12-31T15:26:54","modified_gmt":"2024-12-31T07:26:54","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e6%ba%90%e6%96%87%e4%bb%b6%e6%b8%85%e7%a9%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1059630.html","title":{"rendered":"Python\u4e2d\u5982\u4f55\u5c06\u6e90\u6587\u4ef6\u6e05\u7a7a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/ea160b2a-d941-48b0-9a08-e9b696820881.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u4e2d\u5982\u4f55\u5c06\u6e90\u6587\u4ef6\u6e05\u7a7a\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u6765\u5c06\u6e90\u6587\u4ef6\u6e05\u7a7a\uff1a\u4f7f\u7528\u6587\u4ef6\u6a21\u5f0f &#39;w&#39;\u3001\u5229\u7528 os \u6a21\u5757\u3001\u4f7f\u7528 pathlib \u6a21\u5757\u3001\u8c03\u7528 subprocess \u6a21\u5757\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u5c31\u662f\u4f7f\u7528\u6587\u4ef6\u6a21\u5f0f &#39;w&#39;\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u89e3\u91ca\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u6587\u4ef6\u6a21\u5f0f &#39;w&#39;<\/strong>\uff1a\u5728Python\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6e\u7684 <code>open<\/code> \u51fd\u6570\u4ee5 &#39;w&#39; \u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u53ef\u4ee5\u76f4\u63a5\u6e05\u7a7a\u6587\u4ef6\u7684\u5185\u5bb9\u3002&#39;w&#39; \u6a21\u5f0f\u4ee3\u8868\u5199\u5165\u6a21\u5f0f\uff0c\u5f53\u6587\u4ef6\u4ee5\u8fd9\u79cd\u6a21\u5f0f\u6253\u5f00\u65f6\uff0c\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u5176\u5185\u5bb9\u4f1a\u88ab\u6e05\u7a7a\uff1b\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7a7a\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;filename.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4ee3\u7801\u6bb5\u5c06\u6253\u5f00\u540d\u4e3a <code>filename.txt<\/code> \u7684\u6587\u4ef6\uff0c\u5e76\u6e05\u7a7a\u5176\u5185\u5bb9\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7a7a\u6587\u4ef6\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u63a2\u8ba8\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u6587\u4ef6\u6a21\u5f0f &#39;w&#39;<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u6587\u4ef6\u6a21\u5f0f &#39;w&#39; \u6e05\u7a7a\u6587\u4ef6\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001\u6b65\u9aa4<\/h3>\n<\/p>\n<ol>\n<li>\u4f7f\u7528 <code>open<\/code> \u51fd\u6570\u4ee5 &#39;w&#39; \u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u3002<\/li>\n<li>\u76f4\u63a5\u5173\u95ed\u6587\u4ef6\uff0c\u6216\u8005\u5728 <code>with<\/code> \u8bed\u53e5\u4e2d\u6267\u884c\u3002<\/li>\n<\/ol>\n<p><h3>2\u3001\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ee5 &#39;w&#39; \u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5c06\u5176\u5185\u5bb9\u6e05\u7a7a<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    pass  # \u4e0d\u9700\u8981\u5199\u5165\u4efb\u4f55\u5185\u5bb9\uff0c\u6587\u4ef6\u5185\u5bb9\u4f1a\u88ab\u6e05\u7a7a<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4ee5 &#39;w&#39; \u6a21\u5f0f\u6253\u5f00\u6587\u4ef6 &#39;example.txt&#39;\uff0c\u5e76\u901a\u8fc7 <code>with<\/code> \u8bed\u53e5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u7ed3\u675f\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528 os \u6a21\u5757<\/h2>\n<\/p>\n<p><p>os \u6a21\u5757\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001\u6b65\u9aa4<\/h3>\n<\/p>\n<ol>\n<li>\u4f7f\u7528 <code>os<\/code> \u6a21\u5757\u7684 <code>open<\/code> \u51fd\u6570\u4ee5 &#39;w&#39; \u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u3002<\/li>\n<li>\u5173\u95ed\u6587\u4ef6\uff0c\u6216\u4f7f\u7528 <code>with<\/code> \u8bed\u53e5\u3002<\/li>\n<\/ol>\n<p><h3>2\u3001\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u4ee5 &#39;w&#39; \u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5c06\u5176\u5185\u5bb9\u6e05\u7a7a<\/strong><\/h2>\n<p>with os.fdopen(os.open(&#39;example.txt&#39;, os.O_WRONLY | os.O_TRUNC), &#39;w&#39;) as file:<\/p>\n<p>    pass  # \u4e0d\u9700\u8981\u5199\u5165\u4efb\u4f55\u5185\u5bb9\uff0c\u6587\u4ef6\u5185\u5bb9\u4f1a\u88ab\u6e05\u7a7a<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>os.open<\/code> \u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u7ed3\u5408 <code>os.O_WRONLY<\/code> \u548c <code>os.O_TRUNC<\/code> \u6807\u5fd7\u4ee5\u5199\u6a21\u5f0f\u548c\u622a\u65ad\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528 pathlib \u6a21\u5757<\/h2>\n<\/p>\n<p><p>pathlib \u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u64cd\u4f5c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001\u6b65\u9aa4<\/h3>\n<\/p>\n<ol>\n<li>\u4f7f\u7528 <code>pathlib.Path<\/code> \u7c7b\u521b\u5efa\u6587\u4ef6\u8def\u5f84\u5bf9\u8c61\u3002<\/li>\n<li>\u8c03\u7528 <code>write_text<\/code> \u65b9\u6cd5\u5e76\u4f20\u9012\u7a7a\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ol>\n<p><h3>2\u3001\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u6587\u4ef6\u8def\u5f84\u5bf9\u8c61<\/strong><\/h2>\n<p>file_path = Path(&#39;example.txt&#39;)<\/p>\n<h2><strong>\u5199\u5165\u7a7a\u5b57\u7b26\u4e32\uff0c\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>file_path.write_text(&#39;&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>Path<\/code> \u7c7b\u521b\u5efa\u6587\u4ef6\u8def\u5f84\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7 <code>write_text<\/code> \u65b9\u6cd5\u5199\u5165\u7a7a\u5b57\u7b26\u4e32\u4ee5\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528 subprocess \u6a21\u5757<\/h2>\n<\/p>\n<p><p>subprocess \u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001\u6b65\u9aa4<\/h3>\n<\/p>\n<ol>\n<li>\u4f7f\u7528 <code>subprocess.run<\/code> \u51fd\u6570\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u6765\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\u3002<\/li>\n<\/ol>\n<p><h3>2\u3001\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u4f7f\u7528\u7cfb\u7edf\u547d\u4ee4\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>subprocess.run([&#39;truncate&#39;, &#39;-s&#39;, &#39;0&#39;, &#39;example.txt&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>subprocess.run<\/code> \u51fd\u6570\u6267\u884c <code>truncate<\/code> \u547d\u4ee4\uff0c\u5c06\u6587\u4ef6 &#39;example.txt&#39; \u7684\u5927\u5c0f\u8bbe\u4e3a0\uff0c\u4ece\u800c\u6e05\u7a7a\u5176\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6e90\u6587\u4ef6\u6e05\u7a7a\uff0c\u5305\u62ec\u4f7f\u7528\u6587\u4ef6\u6a21\u5f0f &#39;w&#39;\u3001\u5229\u7528 os \u6a21\u5757\u3001\u4f7f\u7528 pathlib \u6a21\u5757\u3001\u8c03\u7528 subprocess \u6a21\u5757\u3002\u5176\u4e2d\uff0c<strong>\u6587\u4ef6\u6a21\u5f0f &#39;w&#39; \u662f\u6700\u5e38\u7528\u548c\u6700\u7b80\u5355\u7684\u65b9\u6cd5<\/strong>\uff0c\u9002\u5408\u5927\u591a\u6570\u573a\u666f\u3002\u5176\u4ed6\u65b9\u6cd5\u5219\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\uff0c\u9002\u7528\u4e8e\u7279\u5b9a\u9700\u6c42\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u591a\u79cd\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\u7684\u65b9\u6cd5\uff0c\u5e76\u80fd\u591f\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u64cd\u4f5c\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u4f60\u5728Python\u7f16\u7a0b\u4e2d\u66f4\u597d\u5730\u5904\u7406\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6e05\u7a7a\u4e00\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u8981\u6e05\u7a7a\u4e00\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\u3002\u901a\u8fc7\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u4ee5\u5199\u6a21\u5f0f\uff08&#39;w&#39;\uff09\u6253\u5f00\u6587\u4ef6\uff0c\u6587\u4ef6\u7684\u5185\u5bb9\u4f1a\u88ab\u6e05\u7a7a\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;your_file.txt&#39;, &#39;w&#39;) as file:\n    pass  # \u4ec5\u6253\u5f00\u6587\u4ef6\u5e76\u7acb\u5373\u5173\u95ed\uff0c\u5185\u5bb9\u88ab\u6e05\u7a7a\n<\/code><\/pre>\n<p>\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u65f6\u9700\u5c0f\u5fc3\uff0c\u786e\u4fdd\u4e0d\u8bef\u5220\u9664\u91cd\u8981\u6570\u636e\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u65b9\u5f0f\u6e05\u7a7a\u6587\u4ef6\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528\u5199\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>truncate()<\/code>\u65b9\u6cd5\u3002\u9996\u5148\u4ee5\u8bfb\u5199\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u968f\u540e\u8c03\u7528<code>truncate(0)<\/code>\u6765\u6e05\u7a7a\u5185\u5bb9\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;your_file.txt&#39;, &#39;r+&#39;) as file:\n    file.truncate(0)  # \u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728\u6e05\u7a7a\u6587\u4ef6\u540e\u8fdb\u884c\u5176\u4ed6\u64cd\u4f5c\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5728\u6e05\u7a7a\u6587\u4ef6\u524d\u5982\u4f55\u786e\u4fdd\u6570\u636e\u7684\u5b89\u5168\uff1f<\/strong><br \/>\u5728\u6e05\u7a7a\u6587\u4ef6\u4e4b\u524d\uff0c\u5907\u4efd\u6587\u4ef6\u5185\u5bb9\u662f\u4e00\u4e2a\u660e\u667a\u7684\u9009\u62e9\u3002\u53ef\u4ee5\u5c06\u6587\u4ef6\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u4f4d\u7f6e\uff0c\u6216\u4f7f\u7528Python\u5185\u7f6e\u7684<code>shutil<\/code>\u6a21\u5757\u8fdb\u884c\u6587\u4ef6\u590d\u5236\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5907\u4efd\u6587\u4ef6\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import shutil\n\nshutil.copy(&#39;your_file.txt&#39;, &#39;backup_file.txt&#39;)  # \u5907\u4efd\u6587\u4ef6\n<\/code><\/pre>\n<p>\u5728\u8fdb\u884c\u6e05\u7a7a\u64cd\u4f5c\u4e4b\u524d\uff0c\u786e\u4fdd\u6570\u636e\u5b89\u5168\u4f1a\u8ba9\u4f60\u66f4\u52a0\u5b89\u5fc3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u6765\u5c06\u6e90\u6587\u4ef6\u6e05\u7a7a\uff1a\u4f7f\u7528\u6587\u4ef6\u6a21\u5f0f &#39;w&#39;\u3001\u5229\u7528 os \u6a21\u5757\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1059641,"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\/1059630"}],"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=1059630"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1059630\/revisions"}],"predecessor-version":[{"id":1059644,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1059630\/revisions\/1059644"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1059641"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1059630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1059630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1059630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}