Last updated: Apr 10, 2024
Reading timeยท4 min
To check if a line is empty:
file.readlines()
method to get the lines in the file.line.strip()
method to check if the line is empty.# โ check if a line is NOT empty with open('example.txt', 'r', encoding="utf-8") as f: lines = f.readlines() for line in lines: if line.strip(): print('The line is NOT empty ->', line) else: print('The line is empty') print('------------------------------------------') # โ check if a line is empty with open('example.txt', 'r', encoding="utf-8") as f: lines = f.readlines() for line in lines: if line.strip() == "": print('The line is empty') else: print('The line is NOT empty', line)
We used the with statement to open the file in reading mode.
The statement automatically takes care of closing the file for us.
We used the f.readlines()
method to get a list of the lines in the file.
The str.strip method returns a copy of the string with the leading and trailing whitespace (including newline characters) removed.
print(repr('\n'.strip())) # ๐๏ธ '' print(repr(' '.strip())) # ๐๏ธ ''
The method doesn't change the original string, it returns a new string. Strings are immutable in Python.
The first example checks if each line isn't empty.
with open('example.txt', 'r', encoding="utf-8") as f: lines = f.readlines() for line in lines: if line.strip(): print('The line is NOT empty ->', line) else: print('The line is empty')
If the str.strip()
method returns a truthy value (not an empty string), then
the line isn't empty.
To check if a line is empty, compare the result of calling str.strip()
with an
empty string.
with open('example.txt', 'r', encoding="utf-8") as f: lines = f.readlines() for line in lines: if line.strip() == "": print('The line is empty') else: print('The line is NOT empty', line)
If the str.strip()
method returns an empty string, then the line is empty.
This approach returns True
if the line contains whitespace characters or
newlines.
If you only want to check for newline characters, use the in
operator.
This is a three-step process:
file.readlines()
method to get the lines in the file.in
operator to check if the line is empty.# โ check if a line is NOT empty with open('example.txt', 'r', encoding="utf-8") as f: lines = f.readlines() for line in lines: if line not in ['\n', '\r', '\r\n']: print('The line is NOT empty ->', line) else: print('The line is empty') print('------------------------------------------') # โ check if a line is empty with open('example.txt', 'r', encoding="utf-8") as f: lines = f.readlines() for line in lines: if line in ['\n', '\r', '\r\n']: print('The line is empty') else: print('The line is NOT empty', line)
The in operator tests
for membership. For example, x in l
evaluates to True
if x
is a member of
l
, otherwise, it evaluates to False
.
x not in l
returns the negation of x in l
.The first example checks if each line in the file isn't empty.
If the string stored in the line
variable is not equal to \n
, \r
and
\r\n
, then it's not an empty line.
The newline characters are:
\n
for POSIX style encoded files\r\n
for Windows-style encoded files\r
for old Mac encoded filesTo check if the line is empty, use the in
operator instead.
with open('example.txt', 'r', encoding="utf-8") as f: lines = f.readlines() for line in lines: if line in ['\n', '\r', '\r\n']: print('The line is empty') else: print('The line is NOT empty', line)
If the line
variable is one of \n
, \r
or \r\n
, then it's an empty line.
I've also written an article on how to check if user input is empty.
You can learn more about the related topics by checking out the following tutorials: