I just wanted to know if there is a way to get all the lines of code written and then assign it to a variable and finally print it in python?. I'm trying to make a toy benchmark and I need to calculate the productivity
Here is a short routine to count the number of lines in any text file, not just a program module. This is very simplistic--it counts all lines, including blank lines and comments. Continuation lines are treated as separate lines, multiple statements on one line are counted as only one line, etc. I used an f-string for simplicity, so this requires Python 3.6 or above. The print line can easily be modified for other versions of Python.
def count_lines(filename):
with open(filename) as f:
cnt = sum(1 for line in f)
print(f'There are {cnt} lines in {filename}')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.