I would like to know if it is possible to find out how many lines the text of the file contains without using the command:
with open('test.txt') as f: text = f.readlines() size = len(text)
My file is very large, so it’s hard to use this approach ...
More Pythonic will use the generator expression in the function sumas follows:
sum
with open('test.txt') as f: count = sum(1 for _ in f)
Minor modification of your approach
with open('test.txt') as f: line_count = 0 for line in f: line_count += 1 print line_count
Notes:
Here you will go through the lines and will not load the full file into memory
. , , . :
lines = 0 with open('test.txt') as f: for line in f: lines = lines + 1
with open('test.txt') as f: size=len([0 for _ in f])
Source: https://habr.com/ru/post/1607632/More articles:как отключить сообщение об ошибке стека? - jasmineReturns a variable in a Python list with double quotes instead of single quotes - pythonAccessing methods of other models from a custom method in Loopback - node.jsЧто определяет порядок возврата класса .getMethods()? - javaHow to get client secret from azure active directory for own application for using one business application for business? - c #iOS: how to reprogram audio (PCM data) using Audio Unit at runtime? - c ++Elastic search for nested functional indicators and script with function evaluation - elasticsearchWhich of the built-in audio devices can play audio? - iosChange AUGraph Sampling Rate on iOS - iosWhat is ~~ in JavaScript? - javascriptAll Articles