{"id":1174113,"date":"2025-01-15T17:12:45","date_gmt":"2025-01-15T09:12:45","guid":{"rendered":""},"modified":"2025-01-15T17:12:54","modified_gmt":"2025-01-15T09:12:54","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%85%b3%e9%97%ad%e6%96%87%e4%bb%b6%e5%85%b1%e4%ba%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1174113.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5173\u95ed\u6587\u4ef6\u5171\u4eab"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26075721\/2bbaad74-cfc9-4509-98e7-5de6444d0870.webp\" alt=\"python\u4e2d\u5982\u4f55\u5173\u95ed\u6587\u4ef6\u5171\u4eab\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5173\u95ed\u6587\u4ef6\u5171\u4eab\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u6b63\u786e\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f\u3001\u786e\u4fdd\u6240\u6709\u6587\u4ef6\u53e5\u67c4\u90fd\u88ab\u5173\u95ed\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08with\u8bed\u53e5\uff09\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u5373\u4fbf\u5728\u6587\u4ef6\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u5f02\u5e38\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u2014\u2014\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08with \u8bed\u53e5\uff09<\/h3>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u662fPython\u4e2d\u4e00\u79cd\u975e\u5e38\u4fbf\u5229\u7684\u5de5\u5177\uff0c\u5b83\u80fd\u591f\u81ea\u52a8\u7ba1\u7406\u8d44\u6e90\uff0c\u6bd4\u5982\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\u3002\u5728\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = file.read()<\/p>\n<h2><strong>\u6587\u4ef6\u4f1a\u5728\u8fd9\u91cc\u81ea\u52a8\u5173\u95ed\uff0c\u5373\u4fbf\u53d1\u751f\u5f02\u5e38<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>with<\/code>\u8bed\u53e5\u6253\u5f00\u4e86\u4e00\u4e2a\u6587\u4ef6\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf<code>file<\/code>\u3002\u65e0\u8bba\u6587\u4ef6\u64cd\u4f5c\u662f\u5426\u6210\u529f\uff0c\u6587\u4ef6\u90fd\u4f1a\u5728<code>with<\/code>\u5757\u7ed3\u675f\u65f6\u81ea\u52a8\u5173\u95ed\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u5b89\u5168\uff0c\u80fd\u591f\u9632\u6b62\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><h3>\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6253\u5f00\u6587\u4ef6\u65f6\u9700\u8981\u6307\u5b9a\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f\u3002\u5e38\u89c1\u7684\u6a21\u5f0f\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>&#39;r&#39;<\/code>: \u53ea\u8bfb\u6a21\u5f0f<\/li>\n<li><code>&#39;w&#39;<\/code>: \u5199\u6a21\u5f0f\uff08\u8986\u76d6\u73b0\u6709\u5185\u5bb9\uff09<\/li>\n<li><code>&#39;a&#39;<\/code>: \u8ffd\u52a0\u6a21\u5f0f<\/li>\n<li><code>&#39;b&#39;<\/code>: \u4e8c\u8fdb\u5236\u6a21\u5f0f\uff08\u53ef\u4ee5\u4e0e\u5176\u4ed6\u6a21\u5f0f\u7ed3\u5408\u4f7f\u7528\uff09<\/li>\n<\/ul>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u64cd\u4f5c\u7684\u5b89\u5168\u6027\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u53ea\u8bfb\u6a21\u5f0f\u53ef\u4ee5\u9632\u6b62\u610f\u5916\u4fee\u6539\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>try:<\/p>\n<p>    data = file.read()<\/p>\n<p>finally:<\/p>\n<p>    file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>try<\/code>\u5757\u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u800c<code>finally<\/code>\u5757\u5219\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u88ab\u5173\u95ed\uff0c\u5373\u4fbf\u5728\u8bfb\u53d6\u8fc7\u7a0b\u4e2d\u53d1\u751f\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u786e\u4fdd\u6240\u6709\u6587\u4ef6\u53e5\u67c4\u90fd\u88ab\u5173\u95ed<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u786e\u4fdd\u6240\u6709\u6587\u4ef6\u53e5\u67c4\u90fd\u88ab\u5173\u95ed\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u6587\u4ef6\u53e5\u67c4\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u5360\u7528\u8d44\u6e90\uff0c\u5982\u679c\u4e0d\u53ca\u65f6\u5173\u95ed\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8d44\u6e90\u6cc4\u6f0f\u548c\u7cfb\u7edf\u6027\u80fd\u4e0b\u964d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<h2><strong>\u8fdb\u884c\u4e00\u4e9b\u64cd\u4f5c<\/strong><\/h2>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6587\u4ef6\u53e5\u67c4\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u88ab\u663e\u5f0f\u5173\u95ed\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6587\u4ef6\u64cd\u4f5c\uff0c\u4f46\u5728\u590d\u6742\u573a\u666f\u4e2d\u53ef\u80fd\u4e0d\u591f\u5065\u58ee\uff0c\u5efa\u8bae\u7ed3\u5408<code>try<\/code>\u5757\u6216\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u6587\u4ef6\u9501<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u8fdb\u7a0b\u6216\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u6587\u4ef6\u9501\u6765\u63a7\u5236\u6587\u4ef6\u7684\u5171\u4eab\u8bbf\u95ee\u3002Python\u63d0\u4f9b\u4e86<code>fcntl<\/code>\u6a21\u5757\u548c<code>portalocker<\/code>\u5e93\u6765\u5b9e\u73b0\u6587\u4ef6\u9501\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import fcntl<\/p>\n<p>file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>fcntl.flock(file, fcntl.LOCK_EX)<\/p>\n<p>try:<\/p>\n<p>    data = file.read()<\/p>\n<p>finally:<\/p>\n<p>    fcntl.flock(file, fcntl.LOCK_UN)<\/p>\n<p>    file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>fcntl.flock<\/code>\u51fd\u6570\u5bf9\u6587\u4ef6\u8fdb\u884c\u72ec\u5360\u9501\u5b9a\uff0c\u786e\u4fdd\u5728\u64cd\u4f5c\u671f\u95f4\u6ca1\u6709\u5176\u4ed6\u8fdb\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u8be5\u6587\u4ef6\u3002\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u6587\u4ef6\u9501\u4f1a\u88ab\u91ca\u653e\uff0c\u5e76\u4e14\u6587\u4ef6\u53e5\u67c4\u4f1a\u88ab\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h3>\u5176\u4ed6\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5f02\u5e38\u5904\u7406<\/strong>\uff1a\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u6dfb\u52a0\u9002\u5f53\u7684\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002\u4f7f\u7528<code>try<\/code>\u3001<code>except<\/code>\u3001<code>finally<\/code>\u5757\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u53d1\u751f\u5f02\u5e38\u65f6\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/li>\n<li><strong><a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a><\/strong>\uff1a\u5728\u5904\u7406\u5927\u91cf\u6587\u4ef6\u6216\u8fdb\u884c\u590d\u6742\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u786e\u4fdd\u53ca\u65f6\u91ca\u653e\u8d44\u6e90\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u548c\u6587\u4ef6\u9501\u53ef\u4ee5\u6709\u6548\u7ba1\u7406\u8d44\u6e90\uff0c\u9632\u6b62\u8d44\u6e90\u6cc4\u6f0f\u3002<\/li>\n<li><strong>\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b<\/strong>\uff1a\u5728\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u73af\u5883\u4e2d\uff0c\u786e\u4fdd\u6587\u4ef6\u7684\u6b63\u786e\u8bbf\u95ee\u548c\u5173\u95ed\u5c24\u4e3a\u91cd\u8981\u3002\u4f7f\u7528\u6587\u4ef6\u9501\u548c\u540c\u6b65\u673a\u5236\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u64cd\u4f5c\u7684\u5b89\u5168\u6027\u3002<\/li>\n<\/ol>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5173\u95ed\u6587\u4ef6\u5171\u4eab\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\u3002\u63a8\u8350\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u540c\u65f6\u7ed3\u5408\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f\u3001\u5f02\u5e38\u5904\u7406\u548c\u6587\u4ef6\u9501\u7b49\u6280\u672f\u6765\u786e\u4fdd\u6587\u4ef6\u64cd\u4f5c\u7684\u5b89\u5168\u6027\u548c\u8d44\u6e90\u7ba1\u7406\u7684\u6709\u6548\u6027\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u548c\u6280\u672f\uff0c\u53ef\u4ee5\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f\u662f\u6307\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a\u7684\u8bfb\u5199\u6743\u9650\u548c\u65b9\u5f0f\uff0c\u5b83\u4eec\u51b3\u5b9a\u4e86\u5982\u4f55\u5bf9\u6587\u4ef6\u8fdb\u884c\u64cd\u4f5c\u3002\u5e38\u89c1\u7684\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>&#39;r&#39;<\/code>: \u53ea\u8bfb\u6a21\u5f0f<\/li>\n<li><code>&#39;w&#39;<\/code>: \u5199\u6a21\u5f0f\uff08\u8986\u76d6\u73b0\u6709\u5185\u5bb9\uff09<\/li>\n<li><code>&#39;a&#39;<\/code>: \u8ffd\u52a0\u6a21\u5f0f<\/li>\n<li><code>&#39;b&#39;<\/code>: \u4e8c\u8fdb\u5236\u6a21\u5f0f\uff08\u53ef\u4ee5\u4e0e\u5176\u4ed6\u6a21\u5f0f\u7ed3\u5408\u4f7f\u7528\uff09<\/li>\n<li><code>&#39;x&#39;<\/code>: \u6392\u4ed6\u521b\u5efa\u6a21\u5f0f<\/li>\n<\/ul>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u64cd\u4f5c\u7684\u5b89\u5168\u6027\u548c\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u53ea\u8bfb\u6a21\u5f0f <code>&#39;r&#39;<\/code><\/h4>\n<\/p>\n<p><p>\u53ea\u8bfb\u6a21\u5f0f\u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u6587\u4ef6\u5fc5\u987b\u5b58\u5728\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002\u4f7f\u7528\u53ea\u8bfb\u6a21\u5f0f\u53ef\u4ee5\u9632\u6b62\u610f\u5916\u4fee\u6539\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>try:<\/p>\n<p>    data = file.read()<\/p>\n<p>finally:<\/p>\n<p>    file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6587\u4ef6\u4ee5\u53ea\u8bfb\u6a21\u5f0f\u6253\u5f00\uff0c\u786e\u4fdd\u6587\u4ef6\u5185\u5bb9\u4e0d\u4f1a\u88ab\u4fee\u6539\u3002\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u6587\u4ef6\u53e5\u67c4\u88ab\u663e\u5f0f\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h4>\u5199\u6a21\u5f0f <code>&#39;w&#39;<\/code><\/h4>\n<\/p>\n<p><p>\u5199\u6a21\u5f0f\u7528\u4e8e\u5199\u5165\u6587\u4ef6\u5185\u5bb9\uff0c\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u5219\u4f1a\u8986\u76d6\u73b0\u6709\u5185\u5bb9\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;w&#39;)<\/p>\n<p>try:<\/p>\n<p>    file.write(&#39;Hello, World!&#39;)<\/p>\n<p>finally:<\/p>\n<p>    file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6587\u4ef6\u4ee5\u5199\u6a21\u5f0f\u6253\u5f00\uff0c\u5199\u5165\u7684\u5185\u5bb9\u4f1a\u8986\u76d6\u73b0\u6709\u5185\u5bb9\u3002\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u6587\u4ef6\u53e5\u67c4\u88ab\u663e\u5f0f\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h4>\u8ffd\u52a0\u6a21\u5f0f <code>&#39;a&#39;<\/code><\/h4>\n<\/p>\n<p><p>\u8ffd\u52a0\u6a21\u5f0f\u7528\u4e8e\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u5185\u5bb9\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;a&#39;)<\/p>\n<p>try:<\/p>\n<p>    file.write(&#39;\\nHello ag<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n!&#39;)<\/p>\n<p>finally:<\/p>\n<p>    file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6587\u4ef6\u4ee5\u8ffd\u52a0\u6a21\u5f0f\u6253\u5f00\uff0c\u5199\u5165\u7684\u5185\u5bb9\u4f1a\u8ffd\u52a0\u5230\u6587\u4ef6\u672b\u5c3e\u3002\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u6587\u4ef6\u53e5\u67c4\u88ab\u663e\u5f0f\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h4>\u4e8c\u8fdb\u5236\u6a21\u5f0f <code>&#39;b&#39;<\/code><\/h4>\n<\/p>\n<p><p>\u4e8c\u8fdb\u5236\u6a21\u5f0f\u7528\u4e8e\u5904\u7406\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u5982\u56fe\u50cf\u3001\u97f3\u9891\u7b49\u3002\u53ef\u4ee5\u4e0e\u5176\u4ed6\u6a21\u5f0f\u7ed3\u5408\u4f7f\u7528\uff0c\u5982 <code>&#39;rb&#39;<\/code>\uff08\u53ea\u8bfb\u4e8c\u8fdb\u5236\u6a21\u5f0f\uff09\u3001<code>&#39;wb&#39;<\/code>\uff08\u5199\u4e8c\u8fdb\u5236\u6a21\u5f0f\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.bin&#39;, &#39;rb&#39;)<\/p>\n<p>try:<\/p>\n<p>    data = file.read()<\/p>\n<p>finally:<\/p>\n<p>    file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6587\u4ef6\u4ee5\u53ea\u8bfb\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\uff0c\u8bfb\u53d6\u7684\u5185\u5bb9\u662f\u4e8c\u8fdb\u5236\u6570\u636e\u3002\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u6587\u4ef6\u53e5\u67c4\u88ab\u663e\u5f0f\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h4>\u6392\u4ed6\u521b\u5efa\u6a21\u5f0f <code>&#39;x&#39;<\/code><\/h4>\n<\/p>\n<p><p>\u6392\u4ed6\u521b\u5efa\u6a21\u5f0f\u7528\u4e8e\u521b\u5efa\u65b0\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u5219\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;x&#39;)<\/p>\n<p>try:<\/p>\n<p>    file.write(&#39;This is a new file.&#39;)<\/p>\n<p>finally:<\/p>\n<p>    file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6587\u4ef6\u4ee5\u6392\u4ed6\u521b\u5efa\u6a21\u5f0f\u6253\u5f00\uff0c\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u5219\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u6587\u4ef6\u53e5\u67c4\u88ab\u663e\u5f0f\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u662fPython\u4e2d\u7684\u4e00\u4e2a\u5f3a\u5927\u5de5\u5177\uff0c\u5b83\u80fd\u591f\u81ea\u52a8\u7ba1\u7406\u8d44\u6e90\uff0c\u5982\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\u3002\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u5373\u4fbf\u5728\u6587\u4ef6\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5<\/h4>\n<\/p>\n<p><p><code>with<\/code>\u8bed\u53e5\u7528\u4e8e\u7b80\u5316\u8d44\u6e90\u7ba1\u7406\uff0c\u786e\u4fdd\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u91ca\u653e\u8d44\u6e90\u3002\u5bf9\u4e8e\u6587\u4ef6\u64cd\u4f5c\uff0c<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = file.read()<\/p>\n<h2><strong>\u6587\u4ef6\u4f1a\u5728\u8fd9\u91cc\u81ea\u52a8\u5173\u95ed\uff0c\u5373\u4fbf\u53d1\u751f\u5f02\u5e38<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>with<\/code>\u8bed\u53e5\u6253\u5f00\u4e86\u4e00\u4e2a\u6587\u4ef6\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf<code>file<\/code>\u3002\u65e0\u8bba\u6587\u4ef6\u64cd\u4f5c\u662f\u5426\u6210\u529f\uff0c\u6587\u4ef6\u90fd\u4f1a\u5728<code>with<\/code>\u5757\u7ed3\u675f\u65f6\u81ea\u52a8\u5173\u95ed\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u5b89\u5168\uff0c\u80fd\u591f\u9632\u6b62\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><h4>\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u5185\u7f6e\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0cPython\u8fd8\u5141\u8bb8\u6211\u4eec\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u4ee5\u4fbf\u5728\u9700\u8981\u7ba1\u7406\u5176\u4ed6\u8d44\u6e90\u65f6\u4f7f\u7528\u3002\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u9700\u8981\u5b9e\u73b0<code>__enter__<\/code>\u548c<code>__exit__<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class CustomContextManager:<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        print(&#39;Entering context&#39;)<\/p>\n<p>        return self<\/p>\n<p>    def __exit__(self, exc_type, exc_value, traceback):<\/p>\n<p>        print(&#39;Exiting context&#39;)<\/p>\n<p>with CustomContextManager() as manager:<\/p>\n<p>    print(&#39;Inside context&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>CustomContextManager<\/code>\u7c7b\u5b9e\u73b0\u4e86<code>__enter__<\/code>\u548c<code>__exit__<\/code>\u65b9\u6cd5\uff0c\u4ece\u800c\u6210\u4e3a\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3002\u5728<code>with<\/code>\u8bed\u53e5\u5757\u4e2d\uff0c<code>__enter__<\/code>\u65b9\u6cd5\u4f1a\u5728\u8fdb\u5165\u5757\u65f6\u88ab\u8c03\u7528\uff0c\u800c<code>__exit__<\/code>\u65b9\u6cd5\u4f1a\u5728\u79bb\u5f00\u5757\u65f6\u88ab\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u6dfb\u52a0\u9002\u5f53\u7684\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002\u4f7f\u7528<code>try<\/code>\u3001<code>except<\/code>\u3001<code>finally<\/code>\u5757\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u53d1\u751f\u5f02\u5e38\u65f6\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528<code>try<\/code>\u3001<code>except<\/code>\u3001<code>finally<\/code>\u5757<\/h4>\n<\/p>\n<p><p><code>try<\/code>\u5757\u7528\u4e8e\u5305\u542b\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\uff0c\u800c<code>except<\/code>\u5757\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002<code>finally<\/code>\u5757\u7528\u4e8e\u786e\u4fdd\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u90fd\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>try:<\/p>\n<p>    data = file.read()<\/p>\n<p>except IOError as e:<\/p>\n<p>    print(f&quot;An IOError occurred: {e}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6587\u4ef6\u4ee5\u53ea\u8bfb\u6a21\u5f0f\u6253\u5f00\uff0c<code>try<\/code>\u5757\u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u5982\u679c\u53d1\u751f<code>IOError<\/code>\u5f02\u5e38\uff0c\u5219\u4f1a\u5728<code>except<\/code>\u5757\u4e2d\u5904\u7406\u3002\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c<code>finally<\/code>\u5757\u90fd\u4f1a\u786e\u4fdd\u6587\u4ef6\u53e5\u67c4\u88ab\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h4>\u6355\u83b7\u7279\u5b9a\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u591a\u79cd\u7c7b\u578b\u7684\u5f02\u5e38\u3002\u901a\u8fc7\u6355\u83b7\u7279\u5b9a\u7684\u5f02\u5e38\uff0c\u53ef\u4ee5\u66f4\u7cbe\u786e\u5730\u5904\u7406\u9519\u8bef\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>try:<\/p>\n<p>    data = file.read()<\/p>\n<p>except FileNotFoundError as e:<\/p>\n<p>    print(f&quot;File not found: {e}&quot;)<\/p>\n<p>except PermissionError as e:<\/p>\n<p>    print(f&quot;Permission denied: {e}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5206\u522b\u6355\u83b7\u4e86<code>FileNotFoundError<\/code>\u548c<code>PermissionError<\/code>\u5f02\u5e38\uff0c\u5e76\u5728\u76f8\u5e94\u7684<code>except<\/code>\u5757\u4e2d\u5904\u7406\u3002\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c<code>finally<\/code>\u5757\u90fd\u4f1a\u786e\u4fdd\u6587\u4ef6\u53e5\u67c4\u88ab\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6587\u4ef6\u9501<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u8fdb\u7a0b\u6216\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u6587\u4ef6\u9501\u6765\u63a7\u5236\u6587\u4ef6\u7684\u5171\u4eab\u8bbf\u95ee\u3002Python\u63d0\u4f9b\u4e86<code>fcntl<\/code>\u6a21\u5757\u548c<code>portalocker<\/code>\u5e93\u6765\u5b9e\u73b0\u6587\u4ef6\u9501\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528<code>fcntl<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>fcntl<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u6587\u4ef6\u9501\u5b9a\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u6587\u4ef6\u7684\u72ec\u5360\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import fcntl<\/p>\n<p>file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>fcntl.flock(file, fcntl.LOCK_EX)<\/p>\n<p>try:<\/p>\n<p>    data = file.read()<\/p>\n<p>finally:<\/p>\n<p>    fcntl.flock(file, fcntl.LOCK_UN)<\/p>\n<p>    file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>fcntl.flock<\/code>\u51fd\u6570\u5bf9\u6587\u4ef6\u8fdb\u884c\u72ec\u5360\u9501\u5b9a\uff0c\u786e\u4fdd\u5728\u64cd\u4f5c\u671f\u95f4\u6ca1\u6709\u5176\u4ed6\u8fdb\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u8be5\u6587\u4ef6\u3002\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u6587\u4ef6\u9501\u4f1a\u88ab\u91ca\u653e\uff0c\u5e76\u4e14\u6587\u4ef6\u53e5\u67c4\u4f1a\u88ab\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528<code>portalocker<\/code>\u5e93<\/h4>\n<\/p>\n<p><p><code>portalocker<\/code>\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u63d0\u4f9b\u4e86\u8de8\u5e73\u53f0\u7684\u6587\u4ef6\u9501\u5b9a\u529f\u80fd\u3002\u4f7f\u7528<code>portalocker<\/code>\u5e93\u53ef\u4ee5\u7b80\u5316\u6587\u4ef6\u9501\u7684\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import portalocker<\/p>\n<p>file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>portalocker.lock(file, portalocker.LOCK_EX)<\/p>\n<p>try:<\/p>\n<p>    data = file.read()<\/p>\n<p>finally:<\/p>\n<p>    portalocker.unlock(file)<\/p>\n<p>    file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>portalocker.lock<\/code>\u51fd\u6570\u5bf9\u6587\u4ef6\u8fdb\u884c\u72ec\u5360\u9501\u5b9a\uff0c\u786e\u4fdd\u5728\u64cd\u4f5c\u671f\u95f4\u6ca1\u6709\u5176\u4ed6\u8fdb\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u8be5\u6587\u4ef6\u3002\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u6587\u4ef6\u9501\u4f1a\u88ab\u91ca\u653e\uff0c\u5e76\u4e14\u6587\u4ef6\u53e5\u67c4\u4f1a\u88ab\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u8d44\u6e90\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6587\u4ef6\u6216\u8fdb\u884c\u590d\u6742\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u786e\u4fdd\u53ca\u65f6\u91ca\u653e\u8d44\u6e90\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u548c\u6587\u4ef6\u9501\u53ef\u4ee5\u6709\u6548\u7ba1\u7406\u8d44\u6e90\uff0c\u9632\u6b62\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7ba1\u7406\u8d44\u6e90<\/h4>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u6587\u4ef6\u64cd\u4f5c\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u7ba1\u7406\u5176\u4ed6\u7c7b\u578b\u7684\u8d44\u6e90\uff0c\u5982\u6570\u636e\u5e93\u8fde\u63a5\u3001\u7f51\u7edc\u8fde\u63a5\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class DatabaseConnection:<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        self.connection = open_database_connection()<\/p>\n<p>        return self.connection<\/p>\n<p>    def __exit__(self, exc_type, exc_value, traceback):<\/p>\n<p>        self.connection.close()<\/p>\n<p>with DatabaseConnection() as connection:<\/p>\n<p>    # \u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<code>DatabaseConnection<\/code>\u7528\u4e8e\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u3002\u901a\u8fc7<code>with<\/code>\u8bed\u53e5\uff0c\u53ef\u4ee5\u786e\u4fdd\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u6570\u636e\u5e93\u8fde\u63a5\u88ab\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u6587\u4ef6\u9501\u7ba1\u7406\u8d44\u6e90<\/h4>\n<\/p>\n<p><p>\u5728\u591a\u8fdb\u7a0b\u6216\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u4f7f\u7528\u6587\u4ef6\u9501\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u7684\u6b63\u786e\u8bbf\u95ee\u548c\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import fcntl<\/p>\n<p>class LockedFile:<\/p>\n<p>    def __init__(self, filename, mode):<\/p>\n<p>        self.file = open(filename, mode)<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        fcntl.flock(self.file, fcntl.LOCK_EX)<\/p>\n<p>        return self.file<\/p>\n<p>    def __exit__(self, exc_type, exc_value, traceback):<\/p>\n<p>        fcntl.flock(self.file, fcntl.LOCK_UN)<\/p>\n<p>        self.file.close()<\/p>\n<p>with LockedFile(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<code>LockedFile<\/code>\u7528\u4e8e\u7ba1\u7406\u6587\u4ef6\u7684\u72ec\u5360\u8bbf\u95ee\u3002\u901a\u8fc7<code>with<\/code>\u8bed\u53e5\uff0c\u53ef\u4ee5\u786e\u4fdd\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u6587\u4ef6\u9501\u88ab\u91ca\u653e\uff0c\u5e76\u4e14\u6587\u4ef6\u53e5\u67c4\u88ab\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u73af\u5883\u4e2d\uff0c\u786e\u4fdd\u6587\u4ef6\u7684\u6b63\u786e\u8bbf\u95ee\u548c\u5173\u95ed\u5c24\u4e3a\u91cd\u8981\u3002\u4f7f\u7528\u6587\u4ef6\u9501\u548c\u540c\u6b65\u673a\u5236\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u64cd\u4f5c\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u7ebf\u7a0b\u9501<\/h4>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u63d0\u4f9b\u7684\u9501\u673a\u5236\u6765\u540c\u6b65\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>lock = threading.Lock()<\/p>\n<p>def read_file(filename):<\/p>\n<p>    with lock:<\/p>\n<p>        with open(filename, &#39;r&#39;) as file:<\/p>\n<p>            data = file.read()<\/p>\n<p>    return data<\/p>\n<p>thread1 = threading.Thread(target=read_file, args=(&#39;example.txt&#39;,))<\/p>\n<p>thread2 = threading.Thread(target=read_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><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>threading.Lock<\/code>\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u9501\u3002\u5728\u7ebf\u7a0b\u4e2d\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u901a\u8fc7<code>with<\/code>\u8bed\u53e5\u786e\u4fdd\u7ebf\u7a0b\u9501\u7684\u83b7\u53d6\u548c\u91ca\u653e\uff0c\u4ece\u800c\u540c\u6b65\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u8fdb\u7a0b\u9501<\/h4>\n<\/p>\n<p><p>\u5728\u591a\u8fdb\u7a0b\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u7684\u9501\u673a\u5236\u6765\u540c\u6b65\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>lock = multiprocessing.Lock()<\/p>\n<p>def read_file(filename):<\/p>\n<p>    with lock:<\/p>\n<p>        with open(filename, &#39;r&#39;) as file:<\/p>\n<p>            data = file.read()<\/p>\n<p>    return data<\/p>\n<p>process1 = multiprocessing.Process(target=read_file, args=(&#39;example.txt&#39;,))<\/p>\n<p>process2 = multiprocessing.Process(target=read_file, args=(&#39;example.txt&#39;,))<\/p>\n<p>process1.start()<\/p>\n<p>process2.start()<\/p>\n<p>process1.join()<\/p>\n<p>process2.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>multiprocessing.Lock<\/code>\u521b\u5efa\u4e00\u4e2a\u8fdb\u7a0b\u9501\u3002\u5728\u8fdb\u7a0b\u4e2d\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u901a\u8fc7<code>with<\/code>\u8bed\u53e5\u786e\u4fdd\u8fdb\u7a0b\u9501\u7684\u83b7\u53d6\u548c\u91ca\u653e\uff0c\u4ece\u800c\u540c\u6b65\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5173\u95ed\u6587\u4ef6\u5171\u4eab\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\u3002\u63a8\u8350\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u540c\u65f6\u7ed3\u5408\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f\u3001\u5f02\u5e38\u5904\u7406\u548c\u6587\u4ef6\u9501\u7b49\u6280\u672f\u6765\u786e\u4fdd\u6587\u4ef6\u64cd\u4f5c\u7684\u5b89\u5168\u6027\u548c\u8d44\u6e90\u7ba1\u7406\u7684\u6709\u6548\u6027\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u548c\u6280\u672f\uff0c\u53ef\u4ee5\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u6027\u80fd\u3002\u7279\u522b\u662f\u5728\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u73af\u5883\u4e2d\uff0c\u4f7f\u7528\u9501\u673a\u5236\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u64cd\u4f5c\u7684\u540c\u6b65\u6027\uff0c\u4ece\u800c\u907f\u514d\u7ade\u4e89\u6761\u4ef6\u548c\u6570\u636e\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5173\u95ed\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5173\u95ed\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>close()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u786e\u4fdd\u5728\u5b8c\u6210\u6587\u4ef6\u64cd\u4f5c\u540e\u4f7f\u7528\u6b64\u65b9\u6cd5\uff0c\u4ee5\u91ca\u653e\u7cfb\u7edf\u8d44\u6e90\u5e76\u907f\u514d\u6f5c\u5728\u7684\u6570\u636e\u4e22\u5931\u3002\u5982\u679c\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u6253\u5f00\u6587\u4ef6\uff0cPython\u4f1a\u81ea\u52a8\u5904\u7406\u6587\u4ef6\u7684\u5173\u95ed\uff0c\u65e0\u9700\u624b\u52a8\u8c03\u7528<code>close()<\/code>\u3002<\/p>\n<p><strong>\u5173\u95ed\u6587\u4ef6\u540e\u662f\u5426\u4ecd\u53ef\u4ee5\u8bbf\u95ee\u6570\u636e\uff1f<\/strong><br \/>\u4e00\u65e6\u6587\u4ef6\u88ab\u5173\u95ed\uff0c\u60a8\u5c06\u65e0\u6cd5\u518d\u76f4\u63a5\u8bbf\u95ee\u8be5\u6587\u4ef6\u5bf9\u8c61\u7684\u6570\u636e\u3002\u5982\u679c\u9700\u8981\u518d\u6b21\u4f7f\u7528\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff0c\u5fc5\u987b\u91cd\u65b0\u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u56e0\u6b64\uff0c\u786e\u4fdd\u5728\u5173\u95ed\u6587\u4ef6\u4e4b\u524d\u5b8c\u6210\u6240\u6709\u5fc5\u8981\u7684\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u5982\u4f55\u786e\u4fdd\u6587\u4ef6\u5b89\u5168\u5173\u95ed\uff1f<\/strong><br \/>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u4f7f\u7528\u6587\u4ef6\u65f6\u5e94\u6ce8\u610f\u7ebf\u7a0b\u5b89\u5168\u3002\u53ef\u4ee5\u4f7f\u7528\u9501\u673a\u5236\u6765\u786e\u4fdd\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u6587\u4ef6\uff0c\u9632\u6b62\u6570\u636e\u635f\u574f\u3002\u786e\u4fdd\u5728\u6587\u4ef6\u64cd\u4f5c\u5b8c\u6210\u540e\u53ca\u65f6\u91ca\u653e\u9501\uff0c\u4ee5\u4fbf\u5176\u4ed6\u7ebf\u7a0b\u80fd\u591f\u7ee7\u7eed\u6267\u884c\u4efb\u52a1\u3002\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u7b80\u5316\u6b64\u8fc7\u7a0b\uff0c\u5e76\u81ea\u52a8\u5904\u7406\u6587\u4ef6\u7684\u5173\u95ed\u548c\u8d44\u6e90\u7ba1\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5173\u95ed\u6587\u4ef6\u5171\u4eab\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u6b63\u786e\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f\u3001\u786e\u4fdd\u6240\u6709\u6587\u4ef6\u53e5\u67c4\u90fd\u88ab\u5173\u95ed\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668 [&hellip;]","protected":false},"author":3,"featured_media":1174126,"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\/1174113"}],"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=1174113"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174113\/revisions"}],"predecessor-version":[{"id":1174127,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174113\/revisions\/1174127"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1174126"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1174113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1174113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1174113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}