Remove C and C++ comments using Python? - Stack Overflow - python strip comments in a file


Codewars - Kata Retrospective: Strip Comments | Codewars python strip comments in a file

but I've found a sed script here which will remove C/C++ comments from a file. From within Python, it can be used using the following code.

Strip comments and docstrings from a file. """ import sys, token, tokenize. def do_file(fname). """ Run on just one file. """ source = open(fname).

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.