• 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
  • VBA
  • 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.  Python cosh – Find Hyperbolic Cosine of Number Using math.cosh()
  • 2.  pandas sum – Get Sum of Series or DataFrame Columns
  • 3.  Using Python to Get Domain from URL
  • 4.  Using Python Selenium Webdriver to Refresh Page
  • 5.  Convert List to Set with Python
  • 6.  Union of Lists in Python
  • 7.  Get First Character of String in Python
  • 8.  Writing Multiple Lines Lambda Expression in Python
  • 9.  Changing Python Turtle Size with turtlesize() Function
  • 10.  Check if String Contains Only Certain Characters in 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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy