{"id":1181238,"date":"2025-01-15T18:48:35","date_gmt":"2025-01-15T10:48:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1181238.html"},"modified":"2025-01-15T18:48:38","modified_gmt":"2025-01-15T10:48:38","slug":"python%e5%a6%82%e4%bd%95%e7%9f%a5%e9%81%93%e4%bb%a3%e7%a0%81%e7%9a%84%e8%a1%8c%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1181238.html","title":{"rendered":"python\u5982\u4f55\u77e5\u9053\u4ee3\u7801\u7684\u884c\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25125639\/1474578f-ad27-4d27-9959-a73f05ccd591.webp\" alt=\"python\u5982\u4f55\u77e5\u9053\u4ee3\u7801\u7684\u884c\u6570\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u786e\u5b9a\u4ee3\u7801\u7684\u884c\u6570<\/strong>\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u6a21\u5757\u3001\u4ee5\u53ca\u901a\u8fc7\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u5176\u4e2d\u7684\u4e00\u4e9b\u5e38\u89c1\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528<code>len()<\/code>\u548c<code>readlines()<\/code>\u65b9\u6cd5<\/strong>\u8bfb\u53d6\u6587\u4ef6\u5e76\u8ba1\u7b97\u884c\u6570\u3002<\/li>\n<li><strong>\u4f7f\u7528<code>enumerate()<\/code>\u51fd\u6570<\/strong>\u904d\u5386\u6587\u4ef6\u5185\u5bb9\u5e76\u8ba1\u7b97\u884c\u6570\u3002<\/li>\n<li><strong>\u4f7f\u7528<code>sum(1 for line in open(filename))<\/code>\u65b9\u6cd5<\/strong>\u8ba1\u7b97\u6587\u4ef6\u4e2d\u7684\u884c\u6570\u3002<\/li>\n<li><strong>\u501f\u52a9\u7b2c\u4e09\u65b9\u6a21\u5757\u5982<code>pandas<\/code>\u3001<code>numpy<\/code>\u7b49<\/strong>\u6765\u8ba1\u7b97\u4ee3\u7801\u884c\u6570\u3002<\/li>\n<\/ol>\n<p><p>\u6211\u4eec\u6765\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528<code>len()<\/code>\u548c<code>readlines()<\/code>\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>len()<\/code>\u548c<code>readlines()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u6587\u4ef6\uff1a\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\u3002<\/li>\n<li>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff1a\u4f7f\u7528<code>readlines()<\/code>\u65b9\u6cd5\u5c06\u6587\u4ef6\u5185\u5bb9\u8bfb\u5165\u4e00\u4e2a\u5217\u8868\uff0c\u6bcf\u4e00\u884c\u4f5c\u4e3a\u5217\u8868\u7684\u4e00\u4e2a\u5143\u7d20\u3002<\/li>\n<li>\u8ba1\u7b97\u884c\u6570\uff1a\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u8ba1\u7b97\u5217\u8868\u7684\u957f\u5ea6\uff0c\u5373\u4e3a\u6587\u4ef6\u7684\u884c\u6570\u3002<\/li>\n<\/ol>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_lines(filename):<\/p>\n<p>    with open(filename, &#39;r&#39;) as file:<\/p>\n<p>        lines = file.readlines()<\/p>\n<p>        return len(lines)<\/p>\n<p>filename = &#39;example.py&#39;<\/p>\n<p>print(f&quot;The number of lines in the file is: {count_lines(filename)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u540d\u4e3a<code>example.py<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u6587\u4ef6\u5bf9\u8c61\u8d4b\u503c\u7ed9\u53d8\u91cf<code>file<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>readlines()<\/code>\u65b9\u6cd5\u5c06\u6587\u4ef6\u5185\u5bb9\u8bfb\u5165\u4e00\u4e2a\u5217\u8868<code>lines<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u8ba1\u7b97\u5217\u8868\u7684\u957f\u5ea6\uff0c\u5e76\u8fd4\u56de\u8fd9\u4e2a\u503c\u4f5c\u4e3a\u6587\u4ef6\u7684\u884c\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>enumerate()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u6587\u4ef6\uff1a\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\u3002<\/li>\n<li>\u904d\u5386\u6587\u4ef6\u5185\u5bb9\uff1a\u4f7f\u7528<code>enumerate()<\/code>\u51fd\u6570\u904d\u5386\u6587\u4ef6\u5185\u5bb9\uff0c\u540c\u65f6\u8ba1\u6570\u3002<\/li>\n<li>\u8ba1\u7b97\u884c\u6570\uff1a\u901a\u8fc7<code>enumerate()<\/code>\u51fd\u6570\u81ea\u52a8\u8ba1\u6570\u6587\u4ef6\u7684\u884c\u6570\u3002<\/li>\n<\/ol>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_lines(filename):<\/p>\n<p>    with open(filename, &#39;r&#39;) as file:<\/p>\n<p>        for count, line in enumerate(file):<\/p>\n<p>            pass<\/p>\n<p>    return count + 1<\/p>\n<p>filename = &#39;example.py&#39;<\/p>\n<p>print(f&quot;The number of lines in the file is: {count_lines(filename)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u5c06\u6587\u4ef6\u5bf9\u8c61\u8d4b\u503c\u7ed9\u53d8\u91cf<code>file<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>enumerate()<\/code>\u51fd\u6570\u904d\u5386\u6587\u4ef6\u5185\u5bb9\uff0c\u6bcf\u6b21\u8fed\u4ee3\u65f6\uff0c<code>enumerate()<\/code>\u51fd\u6570\u4f1a\u8fd4\u56de\u5f53\u524d\u884c\u7684\u7d22\u5f15<code>count<\/code>\u548c\u884c\u5185\u5bb9<code>line<\/code>\u3002\u6700\u540e\uff0c\u6211\u4eec\u901a\u8fc7<code>count + 1<\/code>\u8ba1\u7b97\u51fa\u6587\u4ef6\u7684\u603b\u884c\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>sum(1 for line in open(filename))<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5229\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6765\u8ba1\u7b97\u6587\u4ef6\u4e2d\u7684\u884c\u6570\uff0c\u7b80\u6d01\u4e14\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_lines(filename):<\/p>\n<p>    return sum(1 for line in open(filename))<\/p>\n<p>filename = &#39;example.py&#39;<\/p>\n<p>print(f&quot;The number of lines in the file is: {count_lines(filename)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<code>(1 for line in open(filename))<\/code>\u6765\u904d\u5386\u6587\u4ef6\u4e2d\u7684\u6bcf\u4e00\u884c\uff0c\u5e76\u4e3a\u6bcf\u4e00\u884c\u751f\u6210\u4e00\u4e2a\u503c<code>1<\/code>\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\u5bf9\u8fd9\u4e9b\u503c\u6c42\u548c\uff0c\u5f97\u5230\u6587\u4ef6\u7684\u603b\u884c\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>pandas<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>pandas<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u548c\u5904\u7406\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u8bfb\u53d6\u6587\u4ef6\u5e76\u8ba1\u7b97\u884c\u6570\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def count_lines(filename):<\/p>\n<p>    df = pd.read_csv(filename)<\/p>\n<p>    return len(df)<\/p>\n<p>filename = &#39;example.csv&#39;<\/p>\n<p>print(f&quot;The number of lines in the file is: {count_lines(filename)}&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>pandas<\/code>\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>pd.read_csv()<\/code>\u51fd\u6570\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5185\u5bb9\u5b58\u50a8\u5728\u6570\u636e\u6846<code>df<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u8ba1\u7b97\u6570\u636e\u6846\u7684\u884c\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>numpy<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>numpy<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u975e\u5e38\u9ad8\u6548\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u8bfb\u53d6\u6587\u4ef6\u5e76\u8ba1\u7b97\u884c\u6570\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def count_lines(filename):<\/p>\n<p>    data = np.loadtxt(filename, delimiter=&#39;,&#39;)<\/p>\n<p>    return data.shape[0]<\/p>\n<p>filename = &#39;example.csv&#39;<\/p>\n<p>print(f&quot;The number of lines in the file is: {count_lines(filename)}&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>numpy<\/code>\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>np.loadtxt()<\/code>\u51fd\u6570\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5185\u5bb9\u5b58\u50a8\u5728\u6570\u7ec4<code>data<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>data.shape[0]<\/code>\u83b7\u53d6\u6570\u7ec4\u7684\u884c\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u8ba1\u7b97\u51faPython\u4ee3\u7801\u6216\u6587\u4ef6\u7684\u884c\u6570<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u52bf\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5c0f\u578b\u6587\u4ef6\uff0c\u53ef\u4ee5\u9009\u62e9<code>len()<\/code>\u548c<code>readlines()<\/code>\u65b9\u6cd5\uff1b\u5bf9\u4e8e\u5927\u578b\u6587\u4ef6\uff0c\u53ef\u4ee5\u9009\u62e9\u751f\u6210\u5668\u8868\u8fbe\u5f0f<code>sum(1 for line in open(filename))<\/code>\u65b9\u6cd5\uff1b\u5bf9\u4e8e\u5904\u7406\u590d\u6742\u6570\u636e\u6587\u4ef6\uff0c\u53ef\u4ee5\u9009\u62e9<code>pandas<\/code>\u6216<code>numpy<\/code>\u7b49\u7b2c\u4e09\u65b9\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6ce8\u610f\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u548c\u5f02\u5e38\u5904\u7406\uff0c\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u65e0\u6cd5\u8bfb\u53d6\u7b49\u60c5\u51b5\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u9ad8\u6548\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u6587\u4ef6\u7f16\u7801\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u53ef\u80fd\u4f1a\u5f71\u54cd\u884c\u6570\u7684\u8ba1\u7b97\u3002\u5982\u679c\u6587\u4ef6\u4f7f\u7528\u975e\u9ed8\u8ba4\u7684\u7f16\u7801\u683c\u5f0f\uff08\u5982UTF-8\u3001GBK\u7b49\uff09\uff0c\u9700\u8981\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u7684<code>encoding<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_lines(filename, encoding=&#39;utf-8&#39;):<\/p>\n<p>    with open(filename, &#39;r&#39;, encoding=encoding) as file:<\/p>\n<p>        lines = file.readlines()<\/p>\n<p>        return len(lines)<\/p>\n<p>filename = &#39;example.py&#39;<\/p>\n<p>print(f&quot;The number of lines in the file is: {count_lines(filename, encoding=&#39;utf-8&#39;)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728<code>open()<\/code>\u51fd\u6570\u4e2d\u6307\u5b9a\u4e86\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u4e3a<code>utf-8<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\u4e0d\u4f1a\u56e0\u4e3a\u7f16\u7801\u95ee\u9898\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6587\u4ef6\u53ef\u80fd\u4e0d\u5b58\u5728\u6216\u65e0\u6cd5\u8bfb\u53d6\uff0c\u56e0\u6b64\u9700\u8981\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_lines(filename, encoding=&#39;utf-8&#39;):<\/p>\n<p>    try:<\/p>\n<p>        with open(filename, &#39;r&#39;, encoding=encoding) as file:<\/p>\n<p>            lines = file.readlines()<\/p>\n<p>            return len(lines)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;The file {filename} does not exist.&quot;)<\/p>\n<p>        return 0<\/p>\n<p>    except IOError:<\/p>\n<p>        print(f&quot;An error occurred while reading the file {filename}.&quot;)<\/p>\n<p>        return 0<\/p>\n<p>filename = &#39;example.py&#39;<\/p>\n<p>print(f&quot;The number of lines in the file is: {count_lines(filename, encoding=&#39;utf-8&#39;)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6355\u83b7\u5e76\u5904\u7406<code>FileNotFoundError<\/code>\u548c<code>IOError<\/code>\u5f02\u5e38\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u65e0\u6cd5\u8bfb\u53d6\uff0c\u4f1a\u8f93\u51fa\u76f8\u5e94\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u5e76\u8fd4\u56de\u884c\u6570\u4e3a0\u3002<\/p>\n<\/p>\n<p><h3>\u591a\u6587\u4ef6\u884c\u6570\u7edf\u8ba1<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7edf\u8ba1\u591a\u4e2a\u6587\u4ef6\u7684\u603b\u884c\u6570\u3002\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u65b9\u6cd5\u8fdb\u884c\u6269\u5c55\uff0c\u904d\u5386\u6587\u4ef6\u5217\u8868\u5e76\u7edf\u8ba1\u6bcf\u4e2a\u6587\u4ef6\u7684\u884c\u6570\uff0c\u6700\u540e\u6c47\u603b\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_lines(filename, encoding=&#39;utf-8&#39;):<\/p>\n<p>    try:<\/p>\n<p>        with open(filename, &#39;r&#39;, encoding=encoding) as file:<\/p>\n<p>            lines = file.readlines()<\/p>\n<p>            return len(lines)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;The file {filename} does not exist.&quot;)<\/p>\n<p>        return 0<\/p>\n<p>    except IOError:<\/p>\n<p>        print(f&quot;An error occurred while reading the file {filename}.&quot;)<\/p>\n<p>        return 0<\/p>\n<p>def count_total_lines(file_list, encoding=&#39;utf-8&#39;):<\/p>\n<p>    total_lines = 0<\/p>\n<p>    for filename in file_list:<\/p>\n<p>        total_lines += count_lines(filename, encoding)<\/p>\n<p>    return total_lines<\/p>\n<p>file_list = [&#39;example1.py&#39;, &#39;example2.py&#39;, &#39;example3.py&#39;]<\/p>\n<p>print(f&quot;The total number of lines in the files is: {count_total_lines(file_list, encoding=&#39;utf-8&#39;)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>count_total_lines()<\/code>\u51fd\u6570\uff0c\u7528\u4e8e\u7edf\u8ba1\u6587\u4ef6\u5217\u8868\u4e2d\u6bcf\u4e2a\u6587\u4ef6\u7684\u884c\u6570\uff0c\u5e76\u6c47\u603b\u7ed3\u679c\u3002\u901a\u8fc7\u904d\u5386\u6587\u4ef6\u5217\u8868\u5e76\u8c03\u7528<code>count_lines()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5f97\u5230\u603b\u884c\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4ee3\u7801\u884c\u6570\u7edf\u8ba1\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u624b\u52a8\u7f16\u5199\u4ee3\u7801\u8ba1\u7b97\u884c\u6570\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u73b0\u6210\u7684\u4ee3\u7801\u884c\u6570\u7edf\u8ba1\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u7edf\u8ba1\u4ee3\u7801\u884c\u6570\u3002\u8fd9\u4e9b\u5de5\u5177\u901a\u5e38\u5177\u6709\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u5982\u7edf\u8ba1\u4ee3\u7801\u884c\u3001\u6ce8\u91ca\u884c\u3001\u7a7a\u884c\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u4ee3\u7801\u884c\u6570\u7edf\u8ba1\u5de5\u5177\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>cloc\uff08Count Lines of Code\uff09<\/strong>\uff1a\u4e00\u4e2a\u5f00\u6e90\u7684\u4ee3\u7801\u884c\u6570\u7edf\u8ba1\u5de5\u5177\uff0c\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u3002<\/li>\n<li><strong>SLOCCount<\/strong>\uff1a\u4e00\u4e2a\u7528\u4e8e\u7edf\u8ba1\u6e90\u4ee3\u7801\u884c\u6570\u7684\u5de5\u5177\uff0c\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u3002<\/li>\n<li><strong>Pylint<\/strong>\uff1a\u4e00\u4e2aPython\u4ee3\u7801\u5206\u6790\u5de5\u5177\uff0c\u9664\u4e86\u4ee3\u7801\u884c\u6570\u7edf\u8ba1\u5916\uff0c\u8fd8\u63d0\u4f9b\u4ee3\u7801\u8d28\u91cf\u68c0\u67e5\u529f\u80fd\u3002<\/li>\n<\/ol>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u548c\u5de5\u5177\u6765\u7edf\u8ba1\u4ee3\u7801\u884c\u6570<\/strong>\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u4ee3\u7801\u884c\u6570\u7684\u7edf\u8ba1\u548c\u7ba1\u7406\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u548c\u5f02\u5e38\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u8bfb\u8005\u5df2\u7ecf\u638c\u63e1\u4e86\u591a\u79cd\u7edf\u8ba1Python\u4ee3\u7801\u884c\u6570\u7684\u65b9\u6cd5\uff0c\u5e76\u4e86\u89e3\u4e86\u5982\u4f55\u5904\u7406\u6587\u4ef6\u7f16\u7801\u548c\u5f02\u5e38\u60c5\u51b5\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u5927\u5bb6\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u66f4\u597d\u5730\u7ba1\u7406\u548c\u7edf\u8ba1\u4ee3\u7801\u884c\u6570\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u4ee3\u7801\u6587\u4ef6\u7684\u603b\u884c\u6570\uff1f<\/strong><br \/>\u8981\u83b7\u53d6Python\u4ee3\u7801\u6587\u4ef6\u7684\u603b\u884c\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>len()<\/code>\u51fd\u6570\u7ed3\u5408<code>readlines()<\/code>\u65b9\u6cd5\u3002\u9996\u5148\u6253\u5f00\u6587\u4ef6\uff0c\u7136\u540e\u8bfb\u53d6\u6240\u6709\u884c\u5e76\u8ba1\u7b97\u884c\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u53ef\u4ee5\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;your_script.py&#39;, &#39;r&#39;) as file:\n    lines = file.readlines()\n    print(len(lines))\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\u6587\u4ef6\u7684\u884c\u6570\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u6392\u9664\u7a7a\u884c\u548c\u6ce8\u91ca\u884c\u6765\u8ba1\u7b97\u6709\u6548\u884c\u6570\uff1f<\/strong><br \/>\u8981\u6392\u9664\u7a7a\u884c\u548c\u6ce8\u91ca\u884c\uff0c\u53ef\u4ee5\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\u8fdb\u884c\u8fc7\u6ee4\u3002\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u6709\u6548\u5730\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;your_script.py&#39;, &#39;r&#39;) as file:\n    valid_lines = [line for line in file if line.strip() and not line.strip().startswith(&#39;#&#39;)]\n    print(len(valid_lines))\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5c06\u8ba1\u7b97\u5e76\u8f93\u51fa\u6709\u6548\u7684\u4ee3\u7801\u884c\u6570\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u5206\u6790Python\u4ee3\u7801\u7684\u884c\u6570\u548c\u590d\u6742\u5ea6\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u6709\u591a\u79cd\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u5206\u6790Python\u4ee3\u7801\u7684\u884c\u6570\u548c\u590d\u6742\u5ea6\u3002\u4f8b\u5982\uff0c<code>Pylint<\/code>\u548c<code>Radon<\/code>\u662f\u4e24\u4e2a\u5e38\u7528\u7684\u9759\u6001\u5206\u6790\u5de5\u5177\u3002\u5b83\u4eec\u4e0d\u4ec5\u63d0\u4f9b\u4ee3\u7801\u884c\u6570\uff0c\u8fd8\u80fd\u591f\u5206\u6790\u4ee3\u7801\u590d\u6742\u5ea6\u3001\u91cd\u590d\u6027\u4ee5\u53ca\u5176\u4ed6\u6f5c\u5728\u95ee\u9898\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u60a8\u53ef\u4ee5\u66f4\u6df1\u5165\u5730\u4e86\u89e3\u4ee3\u7801\u8d28\u91cf\u548c\u7ed3\u6784\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u786e\u5b9a\u4ee3\u7801\u7684\u884c\u6570\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u6a21\u5757\u3001\u4ee5\u53ca\u901a\u8fc7\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f [&hellip;]","protected":false},"author":3,"featured_media":1181245,"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\/1181238"}],"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=1181238"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1181238\/revisions"}],"predecessor-version":[{"id":1181246,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1181238\/revisions\/1181246"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1181245"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1181238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1181238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1181238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}