Hi everyone! After fighting with syntax errors for the last ten minutes, I've finally figured out an elegant one-liner that removes all blank lines from a Python file.
with open('your_file.txt') as file:
lines = list(filter(lambda l: l.strip(), file.readlines()))
for line in lines:
print(line)
I had already been told that the strip()
method was the way to go when determining blank lines, so I simply incorporated it into a lambda
function. Best part in my opinion - it doesn't mutate the original file because it uses filter
.
Make sure to follow for more interesting JS/Python tidbits!
Top comments (0)