• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Get Length of File Using Python

Get Length of File Using Python

June 20, 2022 Leave a Comment

To get the length of a file, or the number of lines in a file, you can use the Python readlines() and len() functions.

with open("example.txt","r") as f:
  print(len(f.readlines()))

#Output:
101

When working with files, the ability to get different statistics about the file easily can be useful.

One such statistic is the length of a file. The length of a file is the number of lines in a given file.

We can get all lines in a file with the readlines() function, and then use the len() function to get the number of lines in the file.

Below is a simple example showing you how to get the length of a file in Python.

with open("example.txt","r") as f:
  print(len(f.readlines()))

#Output:
101

How to Get File Size Using Python

Another common statistic, which may be helpful to know how to get, is the size of a file in bytes.

The Python os module has many great functions which help us interact with the operating system of our computer.

To get the size of a file in Python, you can use the os.path module getsize() function. getsize() returns the size of the file in bytes.

Below is a simple example showing how you can get the size of a file using Python.

import os

print(os.path.getsize("C:/Users/TheProgrammingExpert/example.png"))

#Output:
351

Hopefully this article has been useful for you to learn how to get the length of a file using Python.

Other Articles You'll Also Like:

  • 1.  Convert String to List Using Python
  • 2.  Using Python to Find Maximum Value in List
  • 3.  Check if Variable is None in Python
  • 4.  Selenium maximize_window() Function to Maximize Window in Python
  • 5.  Remove Trailing Zeros from String with rstrip() in Python
  • 6.  How to Rotate a List in Python
  • 7.  Open Multiple Files Using with open in Python
  • 8.  pandas to_pickle – Write DataFrame to Pickle File
  • 9.  Get pandas Series First Element in Python
  • 10.  Sort a List of Strings Using Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy