Page Contents
In this tutorial we will Write a program to delete comment lines from a file in python . We know that any text after “#” in Python is treated as comment . Now we will write a Python program to remove comments from a Python file .
To learn more about Python comments click here
Steps to remove comments from a Python file
- Open the required file in “r” mode using
open()
function - Read all the lines from the file using
readlines()
method and store the list in a variable - Iterate all lines of the list and check if the line contains “#” or not
- If the line starts with “#” then simply remove the line
- But if the line does not start with “#” then check when “#” is found is there any mismatch between “(” and “)” . If no mismatch found then delete the all character after “#” with “#”
- Now open a new file in “W” mode and using
writelines()
method write the changed list to the file .
We have saved the file as “oldfile.py” from which we want to remove comments in the same folder we are working now . Here is the contents of the file –
Python program to remove comments from a Python file
# reading the file
with open("oldfile.py") as fp:
contents=fp.readlines()
# initialize two counter to check mismatch between "(" and ")"
open_bracket_counter=0
close_bracket_counter=0
# whenever an element deleted from the list length of the list will be decreased
decreasing_counter=0
for number in range(len(contents)):
# checking if the line contains "#" or not
if "#" in contents[number-decreasing_counter]:
# delete the line if startswith "#"
if contents[number-decreasing_counter].startswith("#"):
contents.remove(contents[number-decreasing_counter])
decreasing_counter+=1
# delete the character after the "#"
else:
newline=""
for character in contents[number-decreasing_counter]:
if character=="(":
open_bracket_counter+=1
newline+=character
elif character==")":
close_bracket_counter+=1
newline+=character
elif character=="#" and open_bracket_counter==close_bracket_counter:
break
else:
newline+=character
contents.remove(contents[number-decreasing_counter])
contents.insert(number-decreasing_counter,newline)
# writing into a new file
with open("newfile.py","w") as fp:
fp.writelines(contents)
Let’s have a look how the above code will delete comments from a Python file –
- First it will open the “oldfile.py” in “r” mode . After calling
readlines()
method it will return a list['# This is comment1\n', 'print("Hello, #World!") # this is comment2\n', '\n', '# this is comment3']
which will be stored incontents
variable . - Then using for-loop we will check all the line(element) of the list if it contains “#” or not . We can see that 1st , 2nd and 4th line contains “#” .
- As 1st and 4th line starts with “#” so it is deleted by
remove()
method . - But second line contains “#” in the middle . So we need to create a new variable (here it is
newline
) , iterate every character of the second line and add each character to the newline . When program finds first “#” then “(” starts but “)” does not end so loop will not stop . But when program finds second “#” then “(” starts and “)” ends so loop will break . Now we will remove the second line and add thenewline
in that position . So nowcontents
will look like['print("Hello, #World!") ', '\n']
- After that we will open “newfile.py” in “w” mode and write the contents using
writelines()
method .
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .
Good Morning Team, I went through your article in this topic ”Write a program to delete comment lines from a file in Python” the steps comments were to remove from the python file was helpful to know more and gain knowledge. Thank you.