I am having trouble with a really simple problem. I’m trying to remove line breaks in a file and send the output to a new file. This is my code:
infile = open('seq.txt', mode='r', encoding='utf-8')
lines = infile.readlines()
for line in lines:
par = line.strip('\n')
outfile = open('seqpar.txt', mode='w', encoding='utf-8')
for line in lines:
outfile.write(par)
When I run the above code, the contents of the outfile (seqpar.txt) is completely blank. I’ve tried to search for I/O tutorials online, but I can’t seem to locate any solutions to a similar problem. I’m really having trouble with understanding file I/O for some reason.Thanks in advance for the help.
Other answers explain what’s wrong with your code. Here’s a more pythonic way of doing what you’re trying to do: