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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • 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.  Difference Between // and / When Dividing Numbers in Python
  • 2.  How to Ask a Question in Python
  • 3.  pandas to_pickle – Write DataFrame to Pickle File
  • 4.  How to Convert pandas Column dtype from Object to Category
  • 5.  Convert pandas Series to Dictionary in Python
  • 6.  Python issuperset() Function – Check if Set is Superset of Another Set
  • 7.  Using Python to Find Second Largest Value in List
  • 8.  pandas Duplicated – Find Duplicate Rows in DataFrame or Series
  • 9.  How Clear a Set and Remove All Items in Python
  • 10.  Not Equal Operator != 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

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