Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python not writing full string to file

I am attempting to output an aligned protein sequence to a file, however, the output string is truncated each time I attempt to write the string to file. The string I'm attempting to write is 4603 characters long, and is printed to the console with the correct number of characters. The first snippet of code I'm attaching pertains to the output variable itself. I'm going to include the full strings here, because the way in which the strings are truncated upon writing to a file have been reproducible for me.

out=open('out.7.9.12.txt','a+')

score_dict={}
Max='1ZER'
al1='XMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

score_dict[Max]=al1

for name,sequence in score_dict.items():
            out.write('>')
            out.write(name)
            out.write('\n')
            for line in score_dict[name]:
                    out.write(line)
            out.write('\n')

With this code snippet, the output to the file should be equal to al1. However, the output to the file is as follows:

XMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXVXXEKIXXXXPXXXXLIPSTPEXXEMSXEMLXXXXQXXLCXXXSXYXVXXXXXRFXKVPXQXXXXXXXXQIXLQXGXLVDVXXRXXIXPXXXHNXNXXFYRKXLXXXFLXXXXXXXXEIXVXSEXXXXXXKXXSXXXXXRYSXLXXXHXQNMXXXDKIXXXXXXXXXXKVXPTQXXIXXIWXXXXGXXXKQDXXXQVLXXDVXXXXSGADMLXXAKXXXSIAXNCQVXXEXXLXLEXXNCGXHSVVMEXRPRXXXKXTAKLXXIIDFLASVHNTDNNKKLDXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Pasting these two sequences into a word processor and counting the characters will show that the first has 4603 characters and the second has 4090 characters.

I apologize for the sequence blocks here, but I'm not sure how else I could have accurately represented this problem.

EDIT: The code should now work (I fixed the buffer problem and the quotations issue)

like image 398
AHuck Avatar asked Jul 09 '12 15:07

AHuck


3 Answers

Are you closing the file at the end? It may be that some of your data sits in the buffer, so it is good to close() the file when you are done (or flush() it if you don't want to close it for some reason).

Better yet, to use the with construct which will close the file for you when you are done or an exception occurs:

with open('out.7.9.12.txt','a+') as out:
   # the rest of your code indented under here
   # ....
   score_dict={}
   Max=245

Note from the flush() docs:

Note flush() does not necessarily write the file’s data to disk. Use flush() followed by os.fsync() to ensure this behavior

like image 100
Levon Avatar answered Sep 27 '22 23:09

Levon


Use out.flush() at the end (or close() ) to clear buffers.

like image 31
f p Avatar answered Sep 27 '22 23:09

f p


Try putting out.close() at the end of your script. It will ensure that all your output gets flushed to the file.

Also, 'a+' is redundant; 'a' is append-mode which is already writeable.

Max = '245'
al1 = 'XMXXXXXXXXXXXXXXXXXXX ...'
scores = {Max:[al1]}

with open('out.7.9.12.txt', 'a') as outf:
    for name,data in scores.iteritems():
        outf.write('>{}\n{}\n'.format(name, '\n'.join(data)))
like image 32
Hugh Bothwell Avatar answered Sep 27 '22 23:09

Hugh Bothwell