Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid to write '\n' to the last line of a file in python

Tags:

python

file

line

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)

like image 258
Zewei Song Avatar asked Dec 01 '22 15:12

Zewei Song


1 Answers

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.

like image 60
Amir Rachum Avatar answered Dec 09 '22 12:12

Amir Rachum