【问题标题】:Count line total in a file [duplicate]计算文件中的总行数[重复]
【发布时间】:2020-12-05 11:31:44
【问题描述】:

假设我有一个 4 行的文件。在 Python 中,我该如何检查?

鉴于此 sudo 文本文件

line
another line
yet another line
last line

如何遍历该文件以查找行数? Python 有内置的吗?

解决方案: 借助内置的 len 函数,我能够读取总行数,谢谢。

【问题讨论】:

标签: python python-3.x


【解决方案1】:
with open('filename') as f:
    print(len(f.readlines()))

这将打印文件中的行数,因为readlines() 返回文件中的行列表

【讨论】:

  • 工作就像一个魅力,谢谢。一旦计时器允许我接受,我就会接受。
猜你喜欢
  • 2019-08-12
  • 1970-01-01
  • 1970-01-01
  • 2017-12-25
  • 2014-05-24
  • 2016-03-07
  • 1970-01-01
  • 2018-03-19
  • 2021-06-03
相关资源
最近更新 更多