{"id":1182297,"date":"2025-01-15T19:01:38","date_gmt":"2025-01-15T11:01:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1182297.html"},"modified":"2025-01-15T19:01:42","modified_gmt":"2025-01-15T11:01:42","slug":"python%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e5%88%9b%e5%bb%ba%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1182297.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664\u521b\u5efa\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25130341\/661304dc-d037-4d23-bd9a-d0250357377d.webp\" alt=\"python\u5982\u4f55\u5220\u9664\u521b\u5efa\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u5220\u9664\u548c\u521b\u5efa\u6587\u4ef6\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528os\u6a21\u5757\u5220\u9664\u6587\u4ef6\u3001\u4f7f\u7528shutil\u6a21\u5757\u5220\u9664\u6587\u4ef6\u3001\u4f7f\u7528open\u51fd\u6570\u521b\u5efa\u6587\u4ef6\u3001\u4f7f\u7528with open\u8bed\u53e5\u521b\u5efa\u6587\u4ef6<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528os\u6a21\u5757\u5220\u9664\u6587\u4ef6<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528os\u6a21\u5757\u5220\u9664\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u9996\u5148\u9700\u8981\u5bfc\u5165os\u6a21\u5757\uff0c\u53ef\u4ee5\u901a\u8fc7<code>import os<\/code>\u8bed\u53e5\u6765\u5b9e\u73b0\u3002<\/li>\n<li>\u8c03\u7528os\u6a21\u5757\u4e2d\u7684<code>os.remove()<\/code>\u51fd\u6570\uff0c\u4f20\u5165\u8981\u5220\u9664\u7684\u6587\u4ef6\u8def\u5f84\u5373\u53ef\u3002\u4f8b\u5982\uff1a<code>os.remove(&#39;path_to_file&#39;)<\/code>\u3002<\/li>\n<li>\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u629b\u51faFileNotFoundError\u5f02\u5e38\uff0c\u56e0\u6b64\u5efa\u8bae\u5728\u5220\u9664\u6587\u4ef6\u524d\uff0c\u5148\u4f7f\u7528<code>os.path.exists()<\/code>\u51fd\u6570\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if os.path.exists(file_path):<\/p>\n<p>    os.remove(file_path)<\/p>\n<p>    print(f&quot;{file_path} \u5df2\u5220\u9664&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{file_path} \u4e0d\u5b58\u5728&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757\u5220\u9664\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u529f\u80fd\u3002\u5220\u9664\u6587\u4ef6\u662f\u5176\u4e2d\u7684\u4e00\u4e2a\u57fa\u672c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001os.remove()\u51fd\u6570\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>os.remove()\u51fd\u6570\u7528\u4e8e\u5220\u9664\u6307\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\u3002\u5728\u8c03\u7528\u8be5\u51fd\u6570\u65f6\uff0c\u9700\u8981\u4f20\u5165\u6587\u4ef6\u7684\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0cos.remove()\u51fd\u6570\u4f1a\u629b\u51faFileNotFoundError\u5f02\u5e38\uff0c\u56e0\u6b64\u5728\u5220\u9664\u6587\u4ef6\u524d\uff0c\u6700\u597d\u5148\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;test_file.txt&#39;<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if os.path.exists(file_path):<\/p>\n<p>    os.remove(file_path)<\/p>\n<p>    print(f&quot;{file_path} \u5df2\u5220\u9664&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{file_path} \u4e0d\u5b58\u5728&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001os.unlink()\u51fd\u6570\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>os.unlink()\u51fd\u6570\u4e0eos.remove()\u51fd\u6570\u529f\u80fd\u76f8\u540c\uff0c\u4e5f\u7528\u4e8e\u5220\u9664\u6587\u4ef6\u3002\u4f7f\u7528\u65b9\u5f0f\u4e0eos.remove()\u51fd\u6570\u76f8\u540c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;test_file.txt&#39;<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if os.path.exists(file_path):<\/p>\n<p>    os.unlink(file_path)<\/p>\n<p>    print(f&quot;{file_path} \u5df2\u5220\u9664&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{file_path} \u4e0d\u5b58\u5728&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528shutil\u6a21\u5757\u5220\u9664\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>shutil\u6a21\u5757\u662fPython\u4e2d\u7684\u9ad8\u7ea7\u6587\u4ef6\u64cd\u4f5c\u5de5\u5177\uff0c\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c42\u6b21\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u5305\u62ec\u590d\u5236\u3001\u79fb\u52a8\u548c\u5220\u9664\u6587\u4ef6\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001shutil.rmtree()\u51fd\u6570\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>shutil.rmtree()\u51fd\u6570\u7528\u4e8e\u9012\u5f52\u5220\u9664\u76ee\u5f55\u53ca\u5176\u5185\u5bb9\u3002\u5b83\u4f1a\u5220\u9664\u6307\u5b9a\u76ee\u5f55\u53ca\u5176\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u3002\u5982\u679c\u4f60\u9700\u8981\u5220\u9664\u4e00\u4e2a\u76ee\u5f55\u53ca\u5176\u6240\u6709\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528\u8be5\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>dir_path = &#39;test_directory&#39;<\/p>\n<h2><strong>\u68c0\u67e5\u76ee\u5f55\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if os.path.exists(dir_path):<\/p>\n<p>    shutil.rmtree(dir_path)<\/p>\n<p>    print(f&quot;{dir_path} \u5df2\u5220\u9664&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{dir_path} \u4e0d\u5b58\u5728&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001shutil.copy()\u51fd\u6570\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>shutil.copy()\u51fd\u6570\u7528\u4e8e\u590d\u5236\u6587\u4ef6\u3002\u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u6e90\u6587\u4ef6\u8def\u5f84\u548c\u76ee\u6807\u6587\u4ef6\u8def\u5f84\u3002\u4f7f\u7528\u8be5\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6587\u4ef6\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>source_file = &#39;source_file.txt&#39;<\/p>\n<p>destination_file = &#39;destination_file.txt&#39;<\/p>\n<h2><strong>\u590d\u5236\u6587\u4ef6<\/strong><\/h2>\n<p>shutil.copy(source_file, destination_file)<\/p>\n<p>print(f&quot;{source_file} \u5df2\u590d\u5236\u5230 {destination_file}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528open\u51fd\u6570\u521b\u5efa\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>open()\u51fd\u6570\u662fPython\u4e2d\u521b\u5efa\u548c\u64cd\u4f5c\u6587\u4ef6\u7684\u57fa\u672c\u51fd\u6570\u3002\u901a\u8fc7open()\u51fd\u6570\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5e76\u6253\u5f00\u5b83\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u4e00\u4e2a\u7a7a\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u8981\u521b\u5efa\u4e00\u4e2a\u7a7a\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528open()\u51fd\u6570\u5e76\u5c06\u6a21\u5f0f\u8bbe\u7f6e\u4e3a&#39;w&#39;\u6216&#39;a&#39;\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0copen()\u51fd\u6570\u4f1a\u521b\u5efa\u8be5\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u5df2\u7ecf\u5b58\u5728\uff0c\u6a21\u5f0f&#39;w&#39;\u4f1a\u8986\u76d6\u539f\u6587\u4ef6\uff0c\u6a21\u5f0f&#39;a&#39;\u4f1a\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &#39;new_file.txt&#39;<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u6587\u4ef6<\/strong><\/h2>\n<p>with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>    pass<\/p>\n<p>print(f&quot;{file_path} \u5df2\u521b\u5efa&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u5e76\u5199\u5165\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u8981\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5e76\u5199\u5165\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528open()\u51fd\u6570\u5e76\u5c06\u6a21\u5f0f\u8bbe\u7f6e\u4e3a&#39;w&#39;\u6216&#39;a&#39;\uff0c\u7136\u540e\u4f7f\u7528write()\u51fd\u6570\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &#39;new_file.txt&#39;<\/p>\n<p>content = &#39;Hello, World!&#39;<\/p>\n<h2><strong>\u521b\u5efa\u6587\u4ef6\u5e76\u5199\u5165\u5185\u5bb9<\/strong><\/h2>\n<p>with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>    file.write(content)<\/p>\n<p>print(f&quot;{file_path} \u5df2\u521b\u5efa\u5e76\u5199\u5165\u5185\u5bb9&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528with open\u8bed\u53e5\u521b\u5efa\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>with open\u8bed\u53e5\u662f\u4e00\u79cd\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\u64cd\u4f5c\u3002\u4f7f\u7528with open\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u6587\u4ef6\u672a\u5173\u95ed\u5bfc\u81f4\u7684\u8d44\u6e90\u6cc4\u6f0f\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u4e00\u4e2a\u7a7a\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4e0eopen()\u51fd\u6570\u76f8\u540c\uff0c\u53ef\u4ee5\u4f7f\u7528with open\u8bed\u53e5\u521b\u5efa\u4e00\u4e2a\u7a7a\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &#39;new_file.txt&#39;<\/p>\n<h2><strong>\u4f7f\u7528with open\u8bed\u53e5\u521b\u5efa\u4e00\u4e2a\u7a7a\u6587\u4ef6<\/strong><\/h2>\n<p>with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>    pass<\/p>\n<p>print(f&quot;{file_path} \u5df2\u521b\u5efa&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u5e76\u5199\u5165\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u53ef\u4ee5\u4f7f\u7528with open\u8bed\u53e5\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5e76\u5199\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &#39;new_file.txt&#39;<\/p>\n<p>content = &#39;Hello, World!&#39;<\/p>\n<h2><strong>\u4f7f\u7528with open\u8bed\u53e5\u521b\u5efa\u6587\u4ef6\u5e76\u5199\u5165\u5185\u5bb9<\/strong><\/h2>\n<p>with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>    file.write(content)<\/p>\n<p>print(f&quot;{file_path} \u5df2\u521b\u5efa\u5e76\u5199\u5165\u5185\u5bb9&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u6355\u83b7<\/h3>\n<\/p>\n<p><p>\u5728\u5220\u9664\u548c\u521b\u5efa\u6587\u4ef6\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\u548c\u5f02\u5e38\u3002\u4e3a\u4e86\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u5e94\u8be5\u8fdb\u884c\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u6355\u83b7\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5220\u9664\u6587\u4ef6\u65f6\u7684\u5f02\u5e38\u6355\u83b7<\/h4>\n<\/p>\n<p><p>\u5728\u5220\u9664\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u6743\u9650\u4e0d\u8db3\u7b49\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528try&#8230;except\u8bed\u53e5\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;test_file.txt&#39;<\/p>\n<p>try:<\/p>\n<p>    os.remove(file_path)<\/p>\n<p>    print(f&quot;{file_path} \u5df2\u5220\u9664&quot;)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(f&quot;{file_path} \u4e0d\u5b58\u5728&quot;)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(f&quot;\u6ca1\u6709\u6743\u9650\u5220\u9664 {file_path}&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;\u5220\u9664 {file_path} \u65f6\u53d1\u751f\u9519\u8bef\uff1a{e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u6587\u4ef6\u65f6\u7684\u5f02\u5e38\u6355\u83b7<\/h4>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u4e0d\u8db3\u6216\u78c1\u76d8\u7a7a\u95f4\u4e0d\u8db3\u7b49\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528try&#8230;except\u8bed\u53e5\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &#39;new_file.txt&#39;<\/p>\n<p>content = &#39;Hello, World!&#39;<\/p>\n<p>try:<\/p>\n<p>    with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>        file.write(content)<\/p>\n<p>    print(f&quot;{file_path} \u5df2\u521b\u5efa\u5e76\u5199\u5165\u5185\u5bb9&quot;)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(f&quot;\u6ca1\u6709\u6743\u9650\u521b\u5efa {file_path}&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;\u521b\u5efa {file_path} \u65f6\u53d1\u751f\u9519\u8bef\uff1a{e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5220\u9664\u548c\u521b\u5efa\u6587\u4ef6\u662f\u5e38\u89c1\u7684\u64cd\u4f5c\u3002<strong>\u4f7f\u7528os\u6a21\u5757\u5220\u9664\u6587\u4ef6<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7os.remove()\u6216os.unlink()\u51fd\u6570\u5220\u9664\u6587\u4ef6\u3002<strong>\u4f7f\u7528shutil\u6a21\u5757\u53ef\u4ee5\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u6587\u4ef6\u64cd\u4f5c<\/strong>\uff0c\u5982\u9012\u5f52\u5220\u9664\u76ee\u5f55\u53ca\u5176\u5185\u5bb9\u3002<strong>\u4f7f\u7528open\u51fd\u6570\u548cwith open\u8bed\u53e5\u53ef\u4ee5\u521b\u5efa\u548c\u64cd\u4f5c\u6587\u4ef6<\/strong>\uff0c\u63a8\u8350\u4f7f\u7528with open\u8bed\u53e5\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\uff0c\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u3002\u6700\u540e\uff0c\u8fdb\u884c\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u6355\u83b7\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u786e\u4fdd\u7a0b\u5e8f\u5728\u9047\u5230\u9519\u8bef\u65f6\u80fd\u591f\u6b63\u786e\u5904\u7406\u5e76\u63d0\u793a\u7528\u6237\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u5e0c\u671b\u4f60\u5bf9Python\u4e2d\u5220\u9664\u548c\u521b\u5efa\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u4e86\u66f4\u6df1\u5165\u7684\u4e86\u89e3\uff0c\u5e76\u80fd\u591f\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u6587\u4ef6\u5e76\u968f\u540e\u5220\u9664\u5b83\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u521b\u5efa\u6587\u4ef6\uff0c\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>remove()<\/code>\u51fd\u6570\u6765\u5220\u9664\u6587\u4ef6\u3002\u521b\u5efa\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u6587\u4ef6\u7684\u6a21\u5f0f\uff0c\u6bd4\u5982\u5199\u5165\u6a21\u5f0f\uff08&#39;w&#39;\uff09\uff0c\u800c\u5220\u9664\u6587\u4ef6\u5219\u9700\u8981\u63d0\u4f9b\u6587\u4ef6\u7684\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\n# \u521b\u5efa\u6587\u4ef6\nwith open(&#39;example.txt&#39;, &#39;w&#39;) as file:\n    file.write(&#39;Hello, World!&#39;)\n\n# \u5220\u9664\u6587\u4ef6\nos.remove(&#39;example.txt&#39;)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u5220\u9664\u6587\u4ef6\u524d\uff0c\u6587\u4ef6\u5b58\u5728\u4e8e\u6307\u5b9a\u8def\u5f84\u4e2d\u3002<\/p>\n<p><strong>\u5728\u5220\u9664\u6587\u4ef6\u4e4b\u524d\uff0c\u5982\u4f55\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6587\u4ef6\u5220\u9664\u64cd\u4f5c\u4e4b\u524d\uff0c\u5efa\u8bae\u4f7f\u7528<code>os.path.exists()<\/code>\u51fd\u6570\u6765\u786e\u8ba4\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u56e0\u6587\u4ef6\u4e0d\u5b58\u5728\u800c\u5f15\u53d1\u7684\u5f02\u5e38\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">if os.path.exists(&#39;example.txt&#39;):\n    os.remove(&#39;example.txt&#39;)\nelse:\n    print(&quot;\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u65e0\u6cd5\u5220\u9664\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u68c0\u67e5<code>example.txt<\/code>\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u5b58\u5728\u5219\u8fdb\u884c\u5220\u9664\uff0c\u5426\u5219\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<p><strong>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u5220\u9664\u6587\u4ef6\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>os.remove()<\/code>\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u65b9\u6cd5\u6765\u5220\u9664\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.unlink()<\/code>\u51fd\u6570\uff0c\u8fd9\u4e24\u4e2a\u51fd\u6570\u7684\u529f\u80fd\u662f\u76f8\u540c\u7684\u3002\u4f7f\u7528\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">os.unlink(&#39;example.txt&#39;)\n<\/code><\/pre>\n<p>\u65e0\u8bba\u4f7f\u7528<code>remove()<\/code>\u8fd8\u662f<code>unlink()<\/code>\uff0c\u90fd\u80fd\u6709\u6548\u5730\u5220\u9664\u6307\u5b9a\u7684\u6587\u4ef6\u3002\u9009\u62e9\u4f7f\u7528\u54ea\u4e00\u79cd\u65b9\u6cd5\u4e3b\u8981\u53d6\u51b3\u4e8e\u4e2a\u4eba\u7684\u4ee3\u7801\u98ce\u683c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5220\u9664\u548c\u521b\u5efa\u6587\u4ef6\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528os\u6a21\u5757\u5220\u9664\u6587\u4ef6\u3001\u4f7f\u7528shutil\u6a21\u5757\u5220\u9664\u6587\u4ef6\u3001\u4f7f\u7528open\u51fd\u6570 [&hellip;]","protected":false},"author":3,"featured_media":1182312,"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\/1182297"}],"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=1182297"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182297\/revisions"}],"predecessor-version":[{"id":1182318,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182297\/revisions\/1182318"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1182312"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1182297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1182297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1182297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}