{"id":959139,"date":"2024-12-27T03:38:07","date_gmt":"2024-12-26T19:38:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/959139.html"},"modified":"2024-12-27T03:38:09","modified_gmt":"2024-12-26T19:38:09","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80%e5%a4%9a%e4%b8%aa%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/959139.html","title":{"rendered":"python\u5982\u4f55\u6253\u5f00\u591a\u4e2a\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102036\/033fe26a-fd3c-454c-a047-25f3ff1b3976.webp\" alt=\"python\u5982\u4f55\u6253\u5f00\u591a\u4e2a\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u6253\u5f00\u591a\u4e2a\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u3001\u5faa\u73af\u6253\u5f00\u6587\u4ef6\u3001\u5229\u7528<code>fileinput<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>concurrent.futures<\/code>\u5e76\u884c\u5904\u7406\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u662f\u6700\u5e38\u89c1\u4e14\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u5b83\u4e0d\u4ec5\u7b80\u6d01\u6613\u8bfb\uff0c\u8fd8\u80fd\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u8d44\u6e90\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u6765\u6253\u5f00\u591a\u4e2a\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u540c\u65f6\u6253\u5f00\u591a\u4e2a\u6587\u4ef6\uff0c\u5e76\u4e14\u4fdd\u8bc1\u5728\u9000\u51fa\u65f6\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\u3002\u901a\u8fc7\u5728<code>with<\/code>\u8bed\u53e5\u4e2d\u4f7f\u7528\u9017\u53f7\u5206\u9694\u591a\u4e2a<code>open<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u6587\u4ef6\u5bf9\u8c61\uff0c\u8fd9\u6837\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u51cf\u5c11\u4e86\u51fa\u9519\u7684\u6982\u7387\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u9700\u8981\u8bfb\u53d6\u4e24\u4e2a\u6587\u4ef6\u5e76\u5904\u7406\u5176\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;file1.txt&#39;, &#39;r&#39;) as file1, open(&#39;file2.txt&#39;, &#39;r&#39;) as file2:<\/p>\n<p>    content1 = file1.read()<\/p>\n<p>    content2 = file2.read()<\/p>\n<p>    # \u5904\u7406\u8bfb\u53d6\u7684\u5185\u5bb9<\/p>\n<p>    print(content1)<\/p>\n<p>    print(content2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>with<\/code>\u8bed\u53e5\u4f1a\u5728\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u540e\u81ea\u52a8\u5173\u95ed<code>file1<\/code>\u548c<code>file2<\/code>\uff0c\u4ece\u800c\u6709\u6548\u9632\u6b62\u6587\u4ef6\u6cc4\u6f0f\u95ee\u9898\u3002\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u63a2\u8ba8Python\u4e2d\u6253\u5f00\u591a\u4e2a\u6587\u4ef6\u7684\u5176\u4ed6\u65b9\u6cd5\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>with<\/code>\u8bed\u53e5<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u662fPython\u4e2d\u7ba1\u7406\u6587\u4ef6\u64cd\u4f5c\u7684\u6700\u4f73\u5b9e\u8df5\u3002\u5b83\u80fd\u591f\u786e\u4fdd\u5728\u4f7f\u7528\u5b8c\u6587\u4ef6\u540e\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\uff0c\u65e0\u9700\u663e\u5f0f\u8c03\u7528<code>close()<\/code>\u65b9\u6cd5\uff0c\u907f\u514d\u6587\u4ef6\u6cc4\u6f0f\u95ee\u9898\u3002\u901a\u8fc7\u5c06\u591a\u4e2a<code>open<\/code>\u51fd\u6570\u653e\u5728\u540c\u4e00\u4e2a<code>with<\/code>\u8bed\u53e5\u4e2d\uff0c\u53ef\u4ee5\u4e00\u6b21\u6027\u6253\u5f00\u591a\u4e2a\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u540c\u65f6\u6253\u5f00\u591a\u4e2a\u6587\u4ef6\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">with open(&#39;file1.txt&#39;, &#39;r&#39;) as file1, open(&#39;file2.txt&#39;, &#39;r&#39;) as file2:<\/p>\n<p>    # \u8bfb\u53d6\u7b2c\u4e00\u4e2a\u6587\u4ef6<\/p>\n<p>    content1 = file1.read()<\/p>\n<p>    print(&quot;File 1 content:&quot;)<\/p>\n<p>    print(content1)<\/p>\n<p>    # \u8bfb\u53d6\u7b2c\u4e8c\u4e2a\u6587\u4ef6<\/p>\n<p>    content2 = file2.read()<\/p>\n<p>    print(&quot;File 2 content:&quot;)<\/p>\n<p>    print(content2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7<code>with open(&#39;file1.txt&#39;, &#39;r&#39;) as file1, open(&#39;file2.txt&#39;, &#39;r&#39;) as file2:<\/code>\u540c\u65f6\u6253\u5f00\u4e24\u4e2a\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>file1.read()<\/code>\u548c<code>file2.read()<\/code>\u5206\u522b\u8bfb\u53d6\u5b83\u4eec\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5faa\u73af\u6253\u5f00\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u91cf\u8f83\u591a\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u7b80\u5316\u6253\u5f00\u64cd\u4f5c\u3002\u901a\u8fc7\u5c06\u6587\u4ef6\u540d\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u5217\u8868\uff0c\u9010\u4e2a\u6253\u5f00\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528\u5faa\u73af\u6253\u5f00\u591a\u4e2a\u6587\u4ef6\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">file_names = [&#39;file1.txt&#39;, &#39;file2.txt&#39;, &#39;file3.txt&#39;]<\/p>\n<p>file_contents = []<\/p>\n<p>for file_name in file_names:<\/p>\n<p>    with open(file_name, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>        file_contents.append(content)<\/p>\n<p>for i, content in enumerate(file_contents):<\/p>\n<p>    print(f&quot;Content of file {i+1}:&quot;)<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u6587\u4ef6\u540d\u5217\u8868<code>file_names<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u6587\u4ef6\u540d\uff0c\u5e76\u5728\u5faa\u73af\u4e2d\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u6253\u5f00\u6587\u4ef6\u3002\u5c06\u6bcf\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u5b58\u50a8\u5728<code>file_contents<\/code>\u5217\u8868\u4e2d\uff0c\u6700\u540e\u904d\u5386\u6253\u5370\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5229\u7528<code>fileinput<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>fileinput<\/code>\u6a21\u5757\u53ef\u4ee5\u7528\u6765\u5904\u7406\u591a\u4e2a\u8f93\u5165\u6587\u4ef6\uff0c\u5c24\u5176\u9002\u5408\u9700\u8981\u9010\u884c\u8bfb\u53d6\u591a\u4e2a\u6587\u4ef6\u5185\u5bb9\u7684\u60c5\u51b5\u3002\u8be5\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u8fed\u4ee3\u591a\u4e2a\u6587\u4ef6\u7684\u884c\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>fileinput<\/code>\u6a21\u5757\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import fileinput<\/p>\n<p>file_names = [&#39;file1.txt&#39;, &#39;file2.txt&#39;, &#39;file3.txt&#39;]<\/p>\n<p>with fileinput.input(files=file_names) as f:<\/p>\n<p>    for line in f:<\/p>\n<p>        print(f&quot;Line from file {f.filename()}:&quot;, line, end=&#39;&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>fileinput.input()<\/code>\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6587\u4ef6\u540d\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u901a\u8fc7\u904d\u5386\u8fd9\u4e2a\u5bf9\u8c61\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u6bcf\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u3002<code>f.filename()<\/code>\u65b9\u6cd5\u8fd4\u56de\u5f53\u524d\u8bfb\u53d6\u884c\u6240\u5c5e\u7684\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528<code>concurrent.futures<\/code>\u5e76\u884c\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u5e76\u884c\u5904\u7406\u591a\u4e2a\u6587\u4ef6\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>concurrent.futures<\/code>\u6a21\u5757\u63d0\u4f9b\u7684\u5e76\u884c\u6267\u884c\u529f\u80fd\u3002\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6c60\u6216\u8fdb\u7a0b\u6c60\uff0c\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u6587\u4ef6\uff0c\u5927\u5927\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>concurrent.futures<\/code>\u5e76\u884c\u5904\u7406\u591a\u4e2a\u6587\u4ef6\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import concurrent.futures<\/p>\n<p>file_names = [&#39;file1.txt&#39;, &#39;file2.txt&#39;, &#39;file3.txt&#39;]<\/p>\n<p>def read_file(file_name):<\/p>\n<p>    with open(file_name, &#39;r&#39;) as file:<\/p>\n<p>        return file.read()<\/p>\n<p>with concurrent.futures.ThreadPoolExecutor() as executor:<\/p>\n<p>    contents = list(executor.map(read_file, file_names))<\/p>\n<p>for i, content in enumerate(contents):<\/p>\n<p>    print(f&quot;Content of file {i+1}:&quot;)<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u8bfb\u53d6\u6587\u4ef6\u7684\u51fd\u6570<code>read_file()<\/code>\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6c60<code>ThreadPoolExecutor()<\/code>\uff0c\u5e76\u4f7f\u7528<code>executor.map()<\/code>\u65b9\u6cd5\u5c06<code>file_names<\/code>\u5217\u8868\u4e2d\u7684\u6587\u4ef6\u5e76\u884c\u4f20\u9012\u7ed9<code>read_file()<\/code>\u51fd\u6570\u3002\u6700\u540e\uff0c\u904d\u5386\u6253\u5370\u6bcf\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6587\u4ef6<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u7684\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u591a\u4e2a\u6587\u4ef6\u65f6\uff0c\u826f\u597d\u7684\u6587\u4ef6\u8d44\u6e90\u7ba1\u7406\u5c24\u4e3a\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5<\/strong>\uff1a\u59cb\u7ec8\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/li>\n<li><strong>\u5f02\u5e38\u5904\u7406<\/strong>\uff1a\u5728\u6587\u4ef6\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u5404\u79cd\u5f02\u5e38\uff0c\u6bd4\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u8bfb\u53d6\u9519\u8bef\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u5757\u6765\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/li>\n<li><strong>\u6587\u4ef6\u8def\u5f84\u7ba1\u7406<\/strong>\uff1a\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u6765\u7ba1\u7406\u6587\u4ef6\u8def\u5f84\uff0c\u4ee5\u4fbf\u4e8e\u8de8\u5e73\u53f0\u4f7f\u7528\u3002<\/li>\n<li><strong>\u6587\u4ef6\u7f16\u7801\u95ee\u9898<\/strong>\uff1a\u5728\u5904\u7406\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u6ce8\u610f\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u786e\u4fdd\u6b63\u786e\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u3002\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a<code>open<\/code>\u51fd\u6570\u7684<code>encoding<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u6587\u4ef6\u7f16\u7801\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u548c\u5b9e\u8df5\u7ecf\u9a8c\uff0cPython\u7a0b\u5e8f\u5458\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u548c\u5904\u7406\u591a\u4e2a\u6587\u4ef6\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u9ad8\u6548\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u540c\u65f6\u6253\u5f00\u591a\u4e2a\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u6765\u540c\u65f6\u6253\u5f00\u591a\u4e2a\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u5c06\u591a\u4e2a\u6587\u4ef6\u7684\u6253\u5f00\u64cd\u4f5c\u653e\u5728\u540c\u4e00\u4e2a<code>with<\/code>\u8bed\u53e5\u5757\u4e2d\uff0c\u4f7f\u7528\u9017\u53f7\u5206\u9694\u6587\u4ef6\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u4ee3\u7801\u5757\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u6240\u6709\u6587\u4ef6\u90fd\u4f1a\u88ab\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;file1.txt&#39;, &#39;r&#39;) as file1, open(&#39;file2.txt&#39;, &#39;r&#39;) as file2:\n    content1 = file1.read()\n    content2 = file2.read()\n<\/code><\/pre>\n<p><strong>\u5728\u6253\u5f00\u591a\u4e2a\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u5904\u7406\u591a\u4e2a\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u6743\u9650\u4e0d\u8db3\u7b49\u95ee\u9898\u3002\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u53ef\u4ee5\u6709\u6548\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\">try:\n    with open(&#39;file1.txt&#39;, &#39;r&#39;) as file1, open(&#39;file2.txt&#39;, &#39;r&#39;) as file2:\n        content1 = file1.read()\n        content2 = file2.read()\nexcept FileNotFoundError:\n    print(&quot;\u4e00\u4e2a\u6216\u591a\u4e2a\u6587\u4ef6\u672a\u627e\u5230\u3002&quot;)\nexcept PermissionError:\n    print(&quot;\u6ca1\u6709\u6743\u9650\u8bbf\u95ee\u67d0\u4e2a\u6587\u4ef6\u3002&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728\u6253\u5f00\u591a\u4e2a\u6587\u4ef6\u540e\u8fdb\u884c\u6570\u636e\u5904\u7406\uff1f<\/strong><br \/>\u6253\u5f00\u591a\u4e2a\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5bf9\u8bfb\u53d6\u7684\u6570\u636e\u8fdb\u884c\u5404\u79cd\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u6216\u8005\u8fdb\u884c\u6570\u636e\u5206\u6790\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5c06\u591a\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u5408\u5e76\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff1a<\/p>\n<pre><code class=\"language-python\">data = []\nwith open(&#39;file1.txt&#39;, &#39;r&#39;) as file1, open(&#39;file2.txt&#39;, &#39;r&#39;) as file2:\n    data.append(file1.read())\n    data.append(file2.read())\n\n# \u8fdb\u4e00\u6b65\u5904\u7406\u6570\u636e\nfor line in data:\n    print(line)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u7ba1\u7406\u548c\u5904\u7406\u591a\u4e2a\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6253\u5f00\u591a\u4e2a\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528with\u8bed\u53e5\u3001\u5faa\u73af\u6253\u5f00\u6587\u4ef6\u3001\u5229\u7528fileinput\u6a21\u5757\u3001\u4f7f\u7528conc [&hellip;]","protected":false},"author":3,"featured_media":959143,"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\/959139"}],"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=959139"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959139\/revisions"}],"predecessor-version":[{"id":959144,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959139\/revisions\/959144"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/959143"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=959139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=959139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=959139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}