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:

  1. The script gets the filename or path
  2. Opens it and creates an output file (can be changed to another input for the user to type the output file name)
  3. Then it iterates all the lines from the file and for each line it does the following:
  4. Split the line when it has a comment that is identified by //
  5. Then it gets the first element of the split line which is everything that comes before any comment
  6. Now it replaces every space " " with "". This will get rid of all blank spaces.
  7. 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.
  8. 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.
# [email protected]
# 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) > 1:
        l[0] += "\n"
    if l[0].rstrip():

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.

  • \$\begingroup\$ Forgive me for asking but what exactly is your question? Do you want feedback on anything specific in this script? Are you looking for a general solution to remove comments from an assembly file? \$\endgroup\$
    – yuri
    Commented May 24, 2017 at 19:58
  • \$\begingroup\$ @yuri now that you mentioned i realised that the bottom part of my text is missing, it's a simple line asking if there is any mistaken functions or steps that i may have made or a way to improve even more. I will edit it now, thanks. \$\endgroup\$
    – Iago Sousa
    Commented May 24, 2017 at 20:29

1 Answer 1


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:

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.

  • \$\begingroup\$ The main reason i didn't use all functions together was to teach my friend how to use some of them in python with different lists and things. But the "with" function is new to me, thanks for that. As for why i didn't close the files, when a script is handling a file and it gets closed the OS Systems automatically closes a file, and as i were working with a very small project i simply didn't care about it, this doesn't happens on bigger projects. Now i will start using "with" a lot more. \$\endgroup\$
    – Iago Sousa
    Commented May 24, 2017 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.