{"id":973812,"date":"2024-12-27T05:59:56","date_gmt":"2024-12-26T21:59:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/973812.html"},"modified":"2024-12-27T05:59:59","modified_gmt":"2024-12-26T21:59:59","slug":"python%e8%84%9a%e6%9c%ac%e5%a6%82%e4%bd%95%e5%88%a0%e6%8e%89%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/973812.html","title":{"rendered":"python\u811a\u672c\u5982\u4f55\u5220\u6389\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195818\/b319ab9e-3a85-41ff-bf1d-9d3d0302074b.webp\" alt=\"python\u811a\u672c\u5982\u4f55\u5220\u6389\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5220\u9664\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684<code>os<\/code>\u6a21\u5757\u7684<code>remove<\/code>\u51fd\u6570\u6216<code>pathlib<\/code>\u6a21\u5757\u7684<code>unlink<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3001\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u80fd\u6709\u6548\u5220\u9664\u6587\u4ef6\u3002<code>os.remove()<\/code>\u65b9\u6cd5\u76f4\u63a5\u5220\u9664\u6307\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u4f1a\u629b\u51fa<code>FileNotFoundError<\/code>\u5f02\u5e38\u3002<code>pathlib<\/code>\u6a21\u5757\u7684<code>unlink()<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\uff0c\u4e5f\u80fd\u5220\u9664\u6587\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5220\u9664\u6587\u4ef6\uff0c\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>remove<\/code>\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u53ea\u9700\u63d0\u4f9b\u6587\u4ef6\u7684\u8def\u5f84\u5373\u53ef\u5220\u9664\u6587\u4ef6\u3002\u7136\u800c\uff0c\u5728\u8fdb\u884c\u6587\u4ef6\u5220\u9664\u64cd\u4f5c\u65f6\uff0c\u5fc5\u987b\u786e\u4fdd\u6587\u4ef6\u786e\u5b9e\u5b58\u5728\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u4e3a\u4e86\u66f4\u7a33\u5065\u7684\u6587\u4ef6\u5220\u9664\u64cd\u4f5c\uff0c\u901a\u5e38\u7ed3\u5408\u5f02\u5e38\u5904\u7406\u673a\u5236\uff08\u5982<code>try-except<\/code>\u5757\uff09\u6765\u6355\u6349\u6f5c\u5728\u7684\u9519\u8bef\u3002\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u7684<code>unlink()<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u53e6\u4e00\u79cd\u9009\u62e9\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u73b0\u4ee3\u548c\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u64cd\u4f5c\u6587\u4ef6\u8def\u5f84\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528OS\u6a21\u5757\u5220\u9664\u6587\u4ef6<\/p>\n<\/p>\n<p><p>Python\u7684<code>os<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u529f\u80fd\uff0c\u5176\u4e2d<code>remove()<\/code>\u51fd\u6570\u662f\u7528\u4e8e\u5220\u9664\u6587\u4ef6\u7684\u57fa\u672c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528OS\u6a21\u5757\u5220\u9664\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p><code>os.remove()<\/code>\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u5220\u9664\u6587\u4ef6\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u4f7f\u7528\u8be5\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u6587\u4ef6\u7684\u8def\u5f84\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;path\/to\/your\/file.txt&#39;<\/p>\n<p>try:<\/p>\n<p>    os.remove(file_path)<\/p>\n<p>    print(f&quot;File {file_path} has been deleted successfully.&quot;)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(f&quot;File {file_path} not found.&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165<code>os<\/code>\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u8981\u5220\u9664\u7684\u6587\u4ef6\u8def\u5f84\u3002\u901a\u8fc7<code>try-except<\/code>\u5757\uff0c\u6211\u4eec\u6355\u83b7\u53ef\u80fd\u7684\u5f02\u5e38\uff0c\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u7684\u9519\u8bef\uff0c\u5e76\u8f93\u51fa\u76f8\u5e94\u7684\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5220\u9664\u6587\u4ef6\u4e4b\u524d\uff0c\u901a\u5e38\u4f1a\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u9519\u8bef\u3002\u53ef\u4ee5\u4f7f\u7528<code>os.path.exists()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;path\/to\/your\/file.txt&#39;<\/p>\n<p>if os.path.exists(file_path):<\/p>\n<p>    os.remove(file_path)<\/p>\n<p>    print(f&quot;File {file_path} has been deleted successfully.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;File {file_path} does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65b9\u6cd5\u5728\u5220\u9664\u6587\u4ef6\u524d\u8fdb\u884c\u5b58\u5728\u6027\u68c0\u67e5\uff0c\u907f\u514d\u4e86<code>FileNotFoundError<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528PATHLIB\u6a21\u5757\u5220\u9664\u6587\u4ef6<\/p>\n<\/p>\n<p><p>Python 3.4\u5f15\u5165\u4e86<code>pathlib<\/code>\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528PATHLIB\u6a21\u5757\u5220\u9664\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u7684<code>Path<\/code>\u5bf9\u8c61\u5177\u6709<code>unlink()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u6587\u4ef6\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>file_path = Path(&#39;path\/to\/your\/file.txt&#39;)<\/p>\n<p>try:<\/p>\n<p>    file_path.unlink()<\/p>\n<p>    print(f&quot;File {file_path} has been deleted successfully.&quot;)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(f&quot;File {file_path} not found.&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0e<code>os.remove()<\/code>\u7c7b\u4f3c\uff0c\u4f46\u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u5316\u7684\u63a5\u53e3\uff0c\u9002\u5408\u4e0e\u5176\u4ed6<code>pathlib<\/code>\u529f\u80fd\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/strong><\/p>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u5728\u4f7f\u7528<code>pathlib<\/code>\u65f6\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>file_path = Path(&#39;path\/to\/your\/file.txt&#39;)<\/p>\n<p>if file_path.exists():<\/p>\n<p>    file_path.unlink()<\/p>\n<p>    print(f&quot;File {file_path} has been deleted successfully.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;File {file_path} does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0e\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u8fdb\u884c\u68c0\u67e5\u7684\u65b9\u5f0f\u7c7b\u4f3c\uff0c\u4f46\u5229\u7528\u4e86<code>pathlib<\/code>\u7684\u5bf9\u8c61\u63a5\u53e3\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5220\u9664\u591a\u4e2a\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5220\u9664\u76ee\u5f55\u4e0b\u7684\u591a\u4e2a\u6587\u4ef6\uff0c\u53ef\u4ee5\u7ed3\u5408<code>os<\/code>\u6a21\u5757\u6216<code>pathlib<\/code>\u6a21\u5757\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528OS\u6a21\u5757\u5220\u9664\u591a\u4e2a\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\uff0c\u5e76\u9010\u4e2a\u5220\u9664\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>directory_path = &#39;path\/to\/your\/directory&#39;<\/p>\n<p>try:<\/p>\n<p>    for filename in os.listdir(directory_path):<\/p>\n<p>        file_path = os.path.join(directory_path, filename)<\/p>\n<p>        if os.path.isfile(file_path):<\/p>\n<p>            os.remove(file_path)<\/p>\n<p>            print(f&quot;File {file_path} has been deleted.&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>os.listdir()<\/code>\u7528\u4e8e\u83b7\u53d6\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528<code>os.path.isfile()<\/code>\u68c0\u67e5\u6bcf\u4e2a\u6587\u4ef6\u8def\u5f84\uff0c\u786e\u4fdd\u53ea\u5220\u9664\u6587\u4ef6\u800c\u4e0d\u5f71\u54cd\u76ee\u5f55\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528PATHLIB\u6a21\u5757\u5220\u9664\u591a\u4e2a\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u76ee\u5f55\u5220\u9664\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>directory_path = Path(&#39;path\/to\/your\/directory&#39;)<\/p>\n<p>try:<\/p>\n<p>    for file_path in directory_path.iterdir():<\/p>\n<p>        if file_path.is_file():<\/p>\n<p>            file_path.unlink()<\/p>\n<p>            print(f&quot;File {file_path} has been deleted.&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>pathlib<\/code>\u7684<code>iterdir()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u904d\u5386\u76ee\u5f55\u5185\u5bb9\uff0c\u5e76\u901a\u8fc7<code>is_file()<\/code>\u65b9\u6cd5\u786e\u8ba4\u5bf9\u8c61\u662f\u6587\u4ef6\u540e\u8fdb\u884c\u5220\u9664\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5904\u7406\u7279\u6b8a\u60c5\u51b5\u548c\u9519\u8bef<\/p>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u5220\u9664\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\u548c\u9519\u8bef\uff0c\u9700\u8981\u59a5\u5584\u5904\u7406\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5904\u7406\u6587\u4ef6\u9501\u5b9a\u95ee\u9898<\/strong><\/p>\n<\/p>\n<p><p>\u67d0\u4e9b\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u6587\u4ef6\u53ef\u80fd\u88ab\u5176\u4ed6\u7a0b\u5e8f\u9501\u5b9a\u5bfc\u81f4\u5220\u9664\u5931\u8d25\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5173\u95ed\u76f8\u5173\u8fdb\u7a0b\u6216\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u540e\u91cd\u8bd5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6743\u9650\u95ee\u9898<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u6743\u9650\u5220\u9664\u67d0\u4e2a\u6587\u4ef6\uff0c\u4f1a\u5f15\u53d1\u6743\u9650\u9519\u8bef\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\u811a\u672c\u6216\u624b\u52a8\u4fee\u6539\u6587\u4ef6\u6743\u9650\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u5176\u4ed6\u5f02\u5e38<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5220\u9664\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5176\u4ed6\u5f02\u5e38\uff0c\u4f8b\u5982\u8def\u5f84\u683c\u5f0f\u9519\u8bef\u3001\u78c1\u76d8\u7a7a\u95f4\u4e0d\u8db3\u7b49\u3002\u53ef\u4ee5\u901a\u8fc7<code>try-except<\/code>\u5757\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5220\u9664\u6587\u4ef6\u6709\u591a\u79cd\u65b9\u6cd5\uff0c<code>os<\/code>\u6a21\u5757\u7684<code>remove()<\/code>\u65b9\u6cd5\u548c<code>pathlib<\/code>\u6a21\u5757\u7684<code>unlink()<\/code>\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684\u4e24\u79cd\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u786e\u4fdd\u6587\u4ef6\u5b58\u5728\u5e76\u5904\u7406\u6f5c\u5728\u7684\u5f02\u5e38\u662f\u826f\u597d\u7684\u7f16\u7a0b\u5b9e\u8df5\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5220\u9664\u591a\u4e2a\u6587\u4ef6\uff0c\u5e76\u6ce8\u610f\u5904\u7406\u53ef\u80fd\u7684\u7279\u6b8a\u60c5\u51b5\u548c\u9519\u8bef\u3002\u8fd9\u4e9b\u6280\u5de7\u548c\u5b9e\u8df5\u5c06\u5e2e\u52a9\u60a8\u5728Python\u4e2d\u8fdb\u884c\u6709\u6548\u4e14\u5b89\u5168\u7684\u6587\u4ef6\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u811a\u672c\u5220\u9664\u6307\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u5220\u9664\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>remove()<\/code>\u51fd\u6570\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5bfc\u5165<code>os<\/code>\u6a21\u5757\uff0c\u7136\u540e\u8c03\u7528<code>os.remove(&#39;\u6587\u4ef6\u8def\u5f84&#39;)<\/code>\uff0c\u8be5\u51fd\u6570\u5c06\u5220\u9664\u6307\u5b9a\u8def\u5f84\u4e0b\u7684\u6587\u4ef6\u3002\u8bf7\u6ce8\u610f\uff0c\u6587\u4ef6\u5220\u9664\u662f\u4e0d\u53ef\u6062\u590d\u7684\uff0c\u56e0\u6b64\u5728\u6267\u884c\u6b64\u64cd\u4f5c\u524d\uff0c\u8bf7\u786e\u8ba4\u6587\u4ef6\u8def\u5f84\u7684\u51c6\u786e\u6027\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\u5220\u9664\u6587\u4ef6\u4e4b\u524d\uff0c\u5efa\u8bae\u4f7f\u7528<code>os.path.exists()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfile_path = &#39;\u6587\u4ef6\u8def\u5f84&#39;\nif os.path.exists(file_path):\n    os.remove(file_path)\n    print(&quot;\u6587\u4ef6\u5df2\u5220\u9664&quot;)\nelse:\n    print(&quot;\u6587\u4ef6\u4e0d\u5b58\u5728&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u907f\u514d\u56e0\u6587\u4ef6\u4e0d\u5b58\u5728\u800c\u5f15\u53d1\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u811a\u672c\u5220\u9664\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u6267\u884c\u6587\u4ef6\u5220\u9664\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u95ee\u9898\u6216\u6587\u4ef6\u88ab\u5360\u7528\u7b49\u5f02\u5e38\u60c5\u51b5\u3002\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfile_path = &#39;\u6587\u4ef6\u8def\u5f84&#39;\ntry:\n    os.remove(file_path)\n    print(&quot;\u6587\u4ef6\u5df2\u6210\u529f\u5220\u9664&quot;)\nexcept PermissionError:\n    print(&quot;\u6743\u9650\u9519\u8bef\uff1a\u65e0\u6cd5\u5220\u9664\u8be5\u6587\u4ef6&quot;)\nexcept FileNotFoundError:\n    print(&quot;\u6587\u4ef6\u672a\u627e\u5230&quot;)\nexcept Exception as e:\n    print(f&quot;\u53d1\u751f\u9519\u8bef\uff1a{e}&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u9519\u8bef\u5e76\u7ed9\u51fa\u76f8\u5e94\u7684\u63d0\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5220\u9664\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684os\u6a21\u5757\u7684remove\u51fd\u6570\u6216pathlib\u6a21\u5757\u7684unlink\u65b9\u6cd5\u6765\u5b9e [&hellip;]","protected":false},"author":3,"featured_media":973819,"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\/973812"}],"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=973812"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/973812\/revisions"}],"predecessor-version":[{"id":973821,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/973812\/revisions\/973821"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/973819"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=973812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=973812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=973812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}