0

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?

1
  • 2
    Pipe it into a file with your shell: python foo.py > file.txt. Commented Jul 8, 2014 at 23:27

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Replacing sys.stdout is not a good idea. You replace it globally.
@user3817527, you're assigning to sys.stdout multiple times. Try assigning once at the start of the code. There's no need to do it multiple times. Every time you assign it again, you overwrite what used to be in that file (hence only the last iteration's output appears).
@Blender, noted in answer :)
0

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.