How to Count Number of Lines in File with Python
Abstract: count += 1print("Total number of lines iscount)123456fname = "test.txt"count = 0with open(fname
This is a small Python script to count the number of lines in a text file. To run this script you must have Python installed on your system. Now save below script in a Python script (eg: count.py) and update test.txt with your filename in the script to which you need to count lines.
vim count.pyfname = "test.txt" count = 0 with open(fname, 'r') as f: for line in f: count += 1 print("Total number of lines is:", count)123456fname = "test.txt"count = 0with open(fname, 'r') as f: for line in f: count += 1print("Total number of lines is:", count)
Then execute the script on the command line and see the result. This will show you the number of lines available in a file.
python3 count.py Total number of lines is: 513