python – how to find the length of the file in python3?

python – how to find the length of the file in python3?

I stumbled upon the same question while doing a mini project.The below code worked for me:

with open(filename,r) as f:
    print(len(f.readlines()))  # This would give length of files.

I think this is what youre looking for. It iterates over the file line by line.

input_file = open(file_name, r)     
for line in input_file.readlines():
    print(line)

If you want the number of lines in the file, do the following.

lines_in_file = open(file_name, r).readlines()
number_of_lines = len(lines_in_file)

Heres a basic tutorial on file operations for further reading.

python – how to find the length of the file in python3?

Leave a Reply

Your email address will not be published. Required fields are marked *