Skip to main content

How to Ignore Comment Lines When Reading Files

When reading data or configuration files in Python, you often need to ignore comment lines (typically lines starting with #).

This guide presents practical, reliable methods for skipping comment lines while reading files, ensuring your code only processes the relevant data.

Suppose we have the following example.txt file.

tutorial
# reference
#.com
one
# two
three

Ignoring Comment Lines with startswith()

The most straightforward and recommended approach is to use the startswith() method to check if a line begins with the comment character (#):

with open('example.txt', 'r', encoding="utf-8") as f:
for line in f:
line = line.strip() # Remove leading/trailing whitespace
if not line.startswith('#'):
print(line)

Output:

tutorial
one
three
  • The with open(...) statement ensures the file is automatically closed.
  • line.strip() removes leading/trailing whitespace (including newlines), handling blank lines and lines with only whitespace.
  • if not line.startswith('#'): checks if the line doesn't start with a #.
  • If the line doesn't start with #, then print the line.

If you need to handle comment lines differently (e.g. storing them to another list, or processing them, you can easily add it to your code):

with open('example.txt', 'r', encoding="utf-8") as f:
lines = f.readlines()

for line in lines:
line = line.strip()
if not line.startswith('#'):
print(line)
else:
print(f'comment: {line}')

Output:

tutorial
comment: # reference
comment: #.com
one
comment: # two
three
  • You can use the else block to process the comments in any way needed.

You can also use the continue keyword to make the logic even clearer:

with open('example.txt', 'r', encoding="utf-8") as f:
lines = f.readlines()

for line in lines:
line = line.strip()
if line.startswith('#'):
continue # Skip to the next line
print(line)

Output:

tutorial
one
three
  • The continue keyword restarts the loop if the condition in the if statement line.startswith('#') is met.

Ignoring Text After a Comment Character

To ignore only the text after a comment character (allowing inline comments), use partition():

with open('example.txt', 'r', encoding="utf-8") as f:
for line in f:
line = line.partition('#')[0] # Keep only the part before the '#'
line = line.rstrip() # Strip any trailing whitespace
print(line)

Output:

tutorial


one

three
  • line.partition('#') splits the line into three parts: the part before the #, the # itself, and the part after. We only keep the first part ([0]).
  • This allows for lines like data = 123 # This is a comment to be processed correctly (only data = 123 would be kept).

Using shlex (Advanced/Specific Use Cases)

The shlex module (primarily for shell-like syntaxes) can also be used for comment handling, but it's more complex and usually unnecessary for simple comment skipping. It's included here for completeness but is not generally recommended for basic comment removal.

Removing Comments and Newlines

from shlex import shlex

with open('example.txt', 'r', encoding='utf-8') as file_obj:
for line in file_obj:
lex_analyzer = shlex(line)
lex_analyzer.whitespace = '\n' # Treat newlines as whitespace

line = ''.join(list(lex_analyzer)) # Join the remaining tokens

if not line: # Skip if the result is empty.
continue

print(line)

Output:

tutorial
one
three
  • The shlex module creates a lexical analyzer and removes comment lines, while also removing whitespaces.

Splitting Lines into Words While Ignoring Comments

from shlex import split

with open('example.txt', 'r', encoding='utf-8') as file_obj:
for line in file_obj:
line_words = split(line, comments=True) # Splitting the line, skipping comments.
print(line_words)
if not line_words: # Skipping empty lines
continue
print(line_words) # Printing a list of words.

Output:

['tutorial']
['tutorial']
[]
[]
['one']
['one']
[]
['three']
['three']