Using Python to remove blank lines and comments in Assembly code I made this little script to help a friend. He studies computer engineering and is doing a project where he has to make assembly files with comments and some formatting for presentation, but, as he is making a compiler. He explained to me that he needs all their assembly codes without spaces, blank lines or comments and that the other people from his group couldn't find a small and fast solution yet to convert that.

I came up with a simple solution to it using Python:


The script gets the filename or path
Opens it and creates an output file (can be changed to another input for the user to type the output file name)
Then it iterates all the lines from the file and for each line it does the following:
Split the line when it has a comment that is identified by //
Then it gets the first element of the split line which is everything that comes before any comment
Now it replaces every space " " with "". This will get rid of all blank spaces.
If the line had a script the end of the line will be at the second element of the list, this means that the first part will not have a "\n" at the end, do we test if the list has more than one element and if it has we add a "\n" at the end.
I then used rstrip() function to check if the line is not empty, and, if it isn't we write it on the output file.




#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Made by Iago Sousa.
# fciagsousa@gmail.com
# https://github.com/iagsousa/python/blob/master/scripts/removerComentariosAssembly.py

f = raw_input("Insert file name or path: ")
finput = open(f, "r")
foutput = open("saida.txt", "w")
for line in finput:
    l = line.split("//")
    l[0] = l[0].replace(" ", "")
    if len(l) &gt; 1:
        l[0] += "\n"
    if l[0].rstrip():
        foutput.write(l[0])


Input example:



Output example:



Are there any steps that I could've taken to improve my code? Is is optimal for what's intended? Those are my only questions.
I have a few recommendations for your code.

First I noticed that you do not close neither the input nor the output file. This can lead to issues if you or someone else wants to access the files after your script was run. Adding a simple ''finput.close()'' and analogous for the foutput would basically "fix" the issue. However it is considered best practice to use a context manager for accessing resources like files. A context manager is able to open the file for you and close it if you leave the context no matter if you leave it by intent or because of an exception. Using a context manager could look like the following:

with open(input_path) as finput:
    for line in finput:
        print(line)


Another point is how you strip off line endings and what you store in the variables. You say you want to get rid of all comments but nonetheless you are storing them in a variable as well as I would use the strip command before the check if you need to add a new line char. The strip will also remove new line chars and therefore convert empty lines to empty strings one can easily test for. All in all I would write it the following way:

new_line = line.split("//")[0].replace(" ","").rstrip()
if new_line:
    foutput.write(new_line + "\n")


I hope that helps even though I did not add links to the mentioned methods and best practices.