{"id":1148581,"date":"2025-01-13T16:39:08","date_gmt":"2025-01-13T08:39:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1148581.html"},"modified":"2025-01-13T16:39:11","modified_gmt":"2025-01-13T08:39:11","slug":"python-%e7%ba%bf%e7%a8%8b%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1148581.html","title":{"rendered":"python \u7ebf\u7a0b\u5982\u4f55\u5220\u9664\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25172333\/0ad43033-a7c9-446c-840d-0f5d209b6c25.webp\" alt=\"python \u7ebf\u7a0b\u5982\u4f55\u5220\u9664\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python \u7ebf\u7a0b\u5220\u9664\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528os.remove()\u3001\u4f7f\u7528os.unlink()\u3001\u6355\u83b7\u5f02\u5e38\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u6355\u83b7\u5f02\u5e38\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168<\/strong>\u662f\u5173\u952e\uff0c\u56e0\u4e3a\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u5220\u9664\u6587\u4ef6\u65f6\uff0c\u591a\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u540c\u65f6\u8bbf\u95ee\u540c\u4e00\u6587\u4ef6\uff0c\u5bfc\u81f4\u8d44\u6e90\u7ade\u4e89\u548c\u5f02\u5e38\u3002\u4e3a\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\uff0c\u5fc5\u987b\u5728\u5220\u9664\u6587\u4ef6\u65f6\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u5904\u7406\u53ef\u80fd\u53d1\u751f\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u7ebf\u7a0b\u5220\u9664\u6587\u4ef6\u5e76\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001os.remove()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>os.remove()<\/code> \u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u5220\u9664\u6307\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\u3002\u5b83\u662f\u6700\u5e38\u7528\u7684\u5220\u9664\u6587\u4ef6\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def delete_file(file_path):<\/p>\n<p>    try:<\/p>\n<p>        os.remove(file_path)<\/p>\n<p>        print(f&quot;File {file_path} deleted successfully.&quot;)<\/p>\n<p>    except OSError as e:<\/p>\n<p>        print(f&quot;Error: {file_path} : {e.strerror}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>delete_file(&#39;example.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001os.unlink()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>os.unlink()<\/code> \u662f <code>os.remove()<\/code> \u7684\u522b\u540d\uff0c\u529f\u80fd\u5b8c\u5168\u76f8\u540c\u3002\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def delete_file(file_path):<\/p>\n<p>    try:<\/p>\n<p>        os.unlink(file_path)<\/p>\n<p>        print(f&quot;File {file_path} deleted successfully.&quot;)<\/p>\n<p>    except OSError as e:<\/p>\n<p>        print(f&quot;Error: {file_path} : {e.strerror}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>delete_file(&#39;example.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6355\u83b7\u5f02\u5e38\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u5220\u9664\u6587\u4ef6\u65f6\uff0c\u9700\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\u3002\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u5e76\u52a0\u9501\u6765\u9632\u6b62\u8d44\u6e90\u7ade\u4e89\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import threading<\/p>\n<p>lock = threading.Lock()<\/p>\n<p>def delete_file(file_path):<\/p>\n<p>    with lock:<\/p>\n<p>        try:<\/p>\n<p>            os.remove(file_path)<\/p>\n<p>            print(f&quot;File {file_path} deleted successfully.&quot;)<\/p>\n<p>        except OSError as e:<\/p>\n<p>            print(f&quot;Error: {file_path} : {e.strerror}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>thread1 = threading.Thread(target=delete_file, args=(&#39;example.txt&#39;,))<\/p>\n<p>thread2 = threading.Thread(target=delete_file, args=(&#39;example.txt&#39;,))<\/p>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<p>thread1.join()<\/p>\n<p>thread2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u73b0\u7ebf\u7a0b\u5220\u9664\u6587\u4ef6\u7684\u6b65\u9aa4<\/h3>\n<\/p>\n<p><h4>1\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u73b0\u7ebf\u7a0b\u5220\u9664\u6587\u4ef6\u7684\u529f\u80fd\u524d\uff0c\u9700\u5bfc\u5165\u6240\u9700\u6a21\u5757\uff0c\u5305\u62ec <code>os<\/code> \u548c <code>threading<\/code> \u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import threading<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5b9a\u4e49\u5220\u9664\u6587\u4ef6\u7684\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u5220\u9664\u6587\u4ef6\u7684\u51fd\u6570 <code>delete_file()<\/code>\uff0c\u5e76\u5728\u51fd\u6570\u5185\u90e8\u6355\u83b7\u5f02\u5e38\uff0c\u5904\u7406\u5220\u9664\u6587\u4ef6\u65f6\u53ef\u80fd\u9047\u5230\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def delete_file(file_path):<\/p>\n<p>    try:<\/p>\n<p>        os.remove(file_path)<\/p>\n<p>        print(f&quot;File {file_path} deleted successfully.&quot;)<\/p>\n<p>    except OSError as e:<\/p>\n<p>        print(f&quot;Error: {file_path} : {e.strerror}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u521b\u5efa\u7ebf\u7a0b\u5e76\u8c03\u7528\u5220\u9664\u6587\u4ef6\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 <code>threading.Thread<\/code> \u521b\u5efa\u7ebf\u7a0b\uff0c\u5e76\u5c06\u5220\u9664\u6587\u4ef6\u51fd\u6570\u4f5c\u4e3a\u7ebf\u7a0b\u7684\u76ee\u6807\u51fd\u6570\uff0c\u4f20\u5165\u6587\u4ef6\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &#39;example.txt&#39;<\/p>\n<p>thread = threading.Thread(target=delete_file, args=(file_path,))<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\uff0c\u9700\u5728\u5220\u9664\u6587\u4ef6\u51fd\u6570\u4e2d\u52a0\u9501\uff0c\u9632\u6b62\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u540c\u4e00\u6587\u4ef6\u5bfc\u81f4\u8d44\u6e90\u7ade\u4e89\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lock = threading.Lock()<\/p>\n<p>def delete_file(file_path):<\/p>\n<p>    with lock:<\/p>\n<p>        try:<\/p>\n<p>            os.remove(file_path)<\/p>\n<p>            print(f&quot;File {file_path} deleted successfully.&quot;)<\/p>\n<p>        except OSError as e:<\/p>\n<p>            print(f&quot;Error: {file_path} : {e.strerror}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u591a\u7ebf\u7a0b\u5220\u9664\u6587\u4ef6\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u591a\u7ebf\u7a0b\u5220\u9664\u6587\u4ef6\u793a\u4f8b\uff0c\u5305\u62ec\u6240\u6709\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import threading<\/p>\n<p>lock = threading.Lock()<\/p>\n<p>def delete_file(file_path):<\/p>\n<p>    with lock:<\/p>\n<p>        try:<\/p>\n<p>            os.remove(file_path)<\/p>\n<p>            print(f&quot;File {file_path} deleted successfully.&quot;)<\/p>\n<p>        except OSError as e:<\/p>\n<p>            print(f&quot;Error: {file_path} : {e.strerror}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>thread1 = threading.Thread(target=delete_file, args=(file_path,))<\/p>\n<p>thread2 = threading.Thread(target=delete_file, args=(file_path,))<\/p>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<p>thread1.join()<\/p>\n<p>thread2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5220\u9664\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528 <code>os.remove()<\/code> \u6216 <code>os.unlink()<\/code> \u51fd\u6570\u3002\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u5220\u9664\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\u81f3\u5173\u91cd\u8981\u3002\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u5e76\u52a0\u9501\uff0c\u53ef\u4ee5\u9632\u6b62\u8d44\u6e90\u7ade\u4e89\u548c\u5f02\u5e38\uff0c\u786e\u4fdd\u5220\u9664\u6587\u4ef6\u64cd\u4f5c\u7684\u53ef\u9760\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u73af\u5883\u9009\u62e9\u9002\u5f53\u7684\u65b9\u6cd5\u548c\u7b56\u7565\uff0c\u4ee5\u5b9e\u73b0\u9ad8\u6548\u3001\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\u5728Python\u7ebf\u7a0b\u4e2d\u5b89\u5168\u5730\u5220\u9664\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u7684<code>remove<\/code>\u51fd\u6570\u6765\u5220\u9664\u6587\u4ef6\u3002\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u4e3a\u4e86\u907f\u514d\u6587\u4ef6\u88ab\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u64cd\u4f5c\u800c\u5bfc\u81f4\u9519\u8bef\uff0c\u5efa\u8bae\u4f7f\u7528\u7ebf\u7a0b\u9501\uff08<code>threading.Lock<\/code>\uff09\u6765\u786e\u4fdd\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5728\u6267\u884c\u6587\u4ef6\u5220\u9664\u64cd\u4f5c\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728\u5220\u9664\u6587\u4ef6\u4e4b\u524d\u83b7\u53d6\u9501\uff0c\u5e76\u5728\u5220\u9664\u540e\u91ca\u653e\u9501\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Python\u7ebf\u7a0b\u5220\u9664\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6587\u4ef6\u4e0d\u5b58\u5728\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u5c1d\u8bd5\u5220\u9664\u6587\u4ef6\u4e4b\u524d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.exists<\/code>\u51fd\u6570\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u53ef\u4ee5\u9009\u62e9\u8bb0\u5f55\u65e5\u5fd7\u6216\u6253\u5370\u4e00\u6761\u6d88\u606f\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u5c1d\u8bd5\u5220\u9664\u5b83\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5f15\u53d1<code>FileNotFoundError<\/code>\u5f02\u5e38\u3002<\/p>\n<p><strong>Python\u7ebf\u7a0b\u5220\u9664\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u6587\u4ef6\u88ab\u6b63\u786e\u5173\u95ed\uff1f<\/strong><br \/>\u5728\u5220\u9664\u6587\u4ef6\u4e4b\u524d\uff0c\u786e\u4fdd\u6240\u6709\u5bf9\u6587\u4ef6\u7684\u64cd\u4f5c\u90fd\u5df2\u7ecf\u5b8c\u6210\u5e76\u5173\u95ed\u6587\u4ef6\u53e5\u67c4\u3002\u5982\u679c\u4f7f\u7528<code>with open()<\/code>\u8bed\u53e5\u6253\u5f00\u6587\u4ef6\uff0cPython\u4f1a\u81ea\u52a8\u5904\u7406\u6587\u4ef6\u7684\u5173\u95ed\u3002\u8fd9\u53ef\u4ee5\u907f\u514d\u5728\u5220\u9664\u6587\u4ef6\u65f6\u51fa\u73b0\u201c\u6587\u4ef6\u6b63\u5728\u4f7f\u7528\u201d\u7684\u95ee\u9898\uff0c\u786e\u4fdd\u7ebf\u7a0b\u5728\u5220\u9664\u6587\u4ef6\u524d\u6ca1\u6709\u5176\u4ed6\u64cd\u4f5c\u5728\u8fdb\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u7ebf\u7a0b\u5220\u9664\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528os.remove()\u3001\u4f7f\u7528os.unlink()\u3001\u6355\u83b7\u5f02\u5e38\u786e\u4fdd\u7ebf\u7a0b\u5b89 [&hellip;]","protected":false},"author":3,"featured_media":1148583,"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\/1148581"}],"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=1148581"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1148581\/revisions"}],"predecessor-version":[{"id":1148587,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1148581\/revisions\/1148587"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1148583"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1148581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1148581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1148581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}