I'm writing multiple lines to a new file (could be up to several GB), like this:
for item in record:
output_pass.write('%s\n' %item)
However, I got a blank line due to the '\n' of my last record, such as:
Start of the file
record111111
reocrd222222
record333333
---a blank line---
End of a file
Since my file is large, I would not want to read the file again. So, is there an easy way to prevent this, or easy way to remove the last '\n' from the file?
My solution:
Thanks for all the help!
I think I will not load the entire file to the memeory, since it may get very huge.
I actually solve this by first write the first record, then write the rest line in a loop. I put '\n' in the front so it won't appear on the last line.
But Jonathan is right. I actually have now problem with the '\n' in the last line, majorly it is my OCD.
Here is my code:
rec_first = parser_fastq.next() #This is just an iterator of my file
output.write('%s' %('>'+rec_first[0].strip('@')))
output.write('\n%s' %(rec_first[1])) #I put '\n' in the front
count = 1
#Write the rest of lines
for rec_fastq in parser_fastq:
output.write('\n%s' %('>'+rec_fastq[0].strip('@')))
output.write('\n%s' %(rec_fastq[1]))
count += 1
print 'Extracting %ith record in %s ...' %(count, fastq_name) + '\b'*100,
output.close()
print '\n%i records were wrote to %s' % (count, fasta_name)
This should be a simple solution:
for item in record[:-1]:
output_pass.write("%s\n" % item)
output_pass.write("%s" % record[-1])
Using join
is not recommended if you said the file was large - it will create the entire file content string in memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With