• 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 / 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 Check if Number is Divisible by 3 in Python
  • 2.  Multiple Condition if Statements in Python
  • 3.  Shift Values in a List Using Python
  • 4.  Python tostring method – Convert an Object to String with str() Function
  • 5.  Golden Ratio Constant phi in Python
  • 6.  PROC FREQ Equivalent in Python
  • 7.  How to Use Python to Remove Zeros from List
  • 8.  Python cosh – Find Hyperbolic Cosine of Number Using math.cosh()
  • 9.  Adjusting Python Turtle Screen Size with screensize() Function
  • 10.  Get Random Value from Dictionary 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