I would like to iterate through each a file, trim the whitespace for each line, and delete the line if the returned string is empty. Is there a way to avoid duplicating the .strip() call in the list comprehension below? It's not performance-critical but it feels wrong.
sub main():
fname = "foo.txt"
lns = []
with open(fname, 'r') as file:
lns = file.readlines()
newlns = [i.strip() + "\n" for i in lns if i.strip()]
#I want this to look like the following, which doesn't work:
#newlns = [y + "\n" for i in lns if i.strip() as y]
with open("out.txt", 'w') as file:
file.writelines(newlns)