Jlod888
Jlod888

Reputation: 15

How to store the output generated in python in a txt file

So I have a code like this:

disfile = open('filename.txt')
for line in disfile:
    list = line.split()
    id = list[4]
    if id == 'sequence':
        name = list[0]
        Sequence = list[5]
    elif id == 'disorder':
        Disorder = list[5]
        print name, ' ', 'Sequence:',' ', Sequence,' ', 'Disorder:',' ', Disorder

And after that all the data and sequence are printed in the python editor or IDLE. But I want to generate a file which contains all the things that have been printed. Can anyone help me please!

So my file looks like this:

101M : A : sequence MVLSEGEWQLVLHVWAKVEADVAGHGQDILIRLFKSHPETLEKFDRVKHLKTEAEMKASEDLKKHGVTVLTALGAILKKKGHHEAELKPLAQSHATKHKIPIKYLEFISEAIIHVLHSRHPGNFGADAQGAMNKALELFRKDIAAKYKELGYQG 101M : A : secstr HHHHHHHHHHHHHHGGGHHHHHHHHHHHHHHH GGGGGG TTTTT SHHHHHH HHHHHHHHHHHHHHHHHHTTTT HHHHHHHHHHHHHTS HHHHHHHHHHHHHHHHHH GGG SHHHHHHHHHHHHHHHHHHHHHHHHTT
101M : A : disorder ---------------------------------------------------------------------------------------------------------------------------------------------------------- 102L : A : sequence MNIFEMLRIDEGLRLKIYKDTEGYYTIGIGHLLTKSPSLNAAAKSELDKAIGRNTNGVITKDEAEKLFNQDVDAAVRGILRNAKLKPVYDSLDAVRRAALINMVFQMGETGVAGFTNSLRMLQQKRWDEAAVNLAKSRWYNQTPNRAKRVITTFRTGTWDAYKNL 102L : A : secstr HHHHHHHHH EEEEEE TTS EEEETTEEEESSS TTTHHHHHHHHHHTS TTB HHHHHHHHHHHHHHHHHHHHH TTHHHHHHHS HHHHHHHHHHHHHHHHHHHHT HHHHHHHHTT HHHHHHHHHSSHHHHHSHHHHHHHHHHHHHSSSGGG
102L : A : disorder -------------------------------------------------------------------------------------------------------------------------------------------------------------------XX 102M : A : sequence MVLSEGEWQLVLHVWAKVEADVAGHGQDILIRLFKSHPETLEKFDRFKHLKTEAEMKASEDLKKAGVTVLTALGAILKKKGHHEAELKPLAQSHATKHKIPIKYLEFISEAIIHVLHSRHPGNFGADAQGAMNKALELFRKDIAAKYKELGYQG 102M : A : secstr HHHHHHHHHHHHHHGGGHHHHHHHHHHHHHHH GGGGGG TTTTT SHHHHHH HHHHHHHHHHHHHHHHHHTTTT HHHHHHHHHHHHHTS HHHHHHHHHHHHHHHHHH TTT HHHHHHHHHHHHHHHHHHHHHHHHHT
102M : A : disorder ---------------------------------------------------------------------------------------------------------------------------------------------------------- 103L : A : sequence MNIFEMLRIDEGLRLKIYKDTEGYYTIGIGHLLTKSPSLNSLDAAKSELDKAIGRNTNGVITKDEAEKLFNQDVDAAVRGILRNAKLKPVYDSLDAVRRAALINMVFQMGETGVAGFTNSLRMLQQKRWDEAAVNLAKSRWYNQTPNRAKRVITTFRTGTWDAYKNL 103L : A : secstr HHHHHHHHH EEEEEE TTS EEEETTEE HHHHHHHHHHHHTS TTB HHHHHHHHHHHHHHHHHHHHH TTTHHHHHHS HHHHHHHHHHHHHHHHHHHHT HHHHHHHHTT HHHHHHHHHSSHHHHHSHHHHHHHHHHHHHSSSGGG
103L : A : disorder ----------------------------------XXXXXX-----------------------------------------------------------------------------------------------------------------------------XX

And After I added the code:

import sys sys.stdout = open('outputfile', 'w') print 'test' #'test' will be in the output file

Therefore my code becomes:

disfile = open('filename.txt')
for line in disfile:
    list = line.split()
    id = list[4]
    if id == 'sequence':
        name = list[0]
        Sequence = list[5]
    elif id == 'disorder':
        Disorder = list[5]
        import sys
        sys.stdout = open('newfile.txt', 'w')
        print name, ' ', 'Sequence:',' ', Sequence,' ', 'Disorder:',' ', Disorder

It did generate a file, but only the last sequence appeared in the file, what happened?

Upvotes: 0

Views: 1427

Answers (2)

loluengo
loluengo

Reputation: 76

It is only saving the last sequence because on each iteration it is opening the file again and overwriting. You should open it before the loop.

But I'd recommend using writes or print "chevron" format. i.e.:

f = open('outputfile', 'w')
for .....
    print >>f,name, ' ', 'Sequence:',' ', Sequence,' ', 'Disorder:',' ', Disorder

Upvotes: 0

sushain97
sushain97

Reputation: 2802

You can either write to a file you open using write() or redirect stdout so that all the output via print will go to a file:

import sys
sys.stdout = open('outputfile', 'w')
print 'test' #'test' will be in the outputfile

You can also pipe into a file as Blender mentioned in a comment using > however that only applies if you're using a shell rather than the Python editor or IDLE as you mentioned.

Replacing stdout isn't something that should be done in production code since it is replaced globally, i.e. the output of other functions external to your code will also end up in the file. You might want to save the original sys.stdout if you do replace it.

Upvotes: 1

Related Questions