• 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 / Check if Line is Empty in Python

Check if Line is Empty in Python

July 6, 2022 Leave a Comment

To check if a line is empty when reading from a file in Python, the easiest way is by checking if the line is equal to newline characters.

myfile = open("example.txt", "r")

lines = myfile.readlines()

for line in lines:
    if line in ['\n','\r\n']: 
        #line is empty...

You can check if a line is not empty with the strip() function.

myfile = open("example.txt", "r")

lines = myfile.readlines()

for line in lines:
    if line.split(): 
        #line is not empty...

When working with files, if you have bad inputs, you can have some headaches. One such situation is if you have empty lines and whitespace in your files.

You can check if a line is empty or not in Python easily.

To check if a line is empty when reading from a file in Python, the easiest way is by checking if the line is equal to newline characters.

Below is a simple example showing you how to check if a line is empty in Python.

myfile = open("example.txt", "r")

lines = myfile.readlines()

for line in lines:
    if line in ['\n','\r\n']: 
        #line is empty...

How to Check if Line is Not Empty in Python

If you are going the other way and only want to perform certain operations when a line is not empty, then you can use the Python strip() function in an if statement.

If the stripped line has no length, then the resulting boolean value will be False.

So, you can use strip() to see if the line has any characters and if it does, then you know it’s not empty.

Below is a simple example showing you how to check if a line is empty in Python.

myfile = open("example.txt", "r")

lines = myfile.readlines()

for line in lines:
    if line.split(): 
        #line is not empty...

Hopefully this article has been useful for you to learn how to check if a line is empty in Python.

Other Articles You'll Also Like:

  • 1.  How to Combine Dictionaries in Python
  • 2.  Scroll Down Using Selenium in Python
  • 3.  Using Python to Count Items in List Matching Criteria
  • 4.  Using Python to Find Second Smallest Value in List
  • 5.  PROC PHREG Equivalent in Python
  • 6.  How to Write Python Dictionary to File
  • 7.  Convert First Letter of String to Lowercase in Python
  • 8.  Python atanh – Find Hyperbolic Arctangent of Number Using math.atanh()
  • 9.  e in Python – Using Math Module to Get Euler’s Constant e
  • 10.  Using Python to Append Character to String Variable

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