strip_comments(src, dst) that copies a script from src to dst, omitting every line whose first non-whitespace character is #. All other lines (including blank ones) are copied unchanged. Return the number of lines written.strip_comments('in.py', 'out.py') # in.py: 'x = 1\n# note\n # indented\ny = 2\n'2
Both comment lines vanish; two code lines are written.
line.lstrip().startswith('#') detects comments.
Open both files in one with statement: with open(a) as f, open(b, "w") as g.