1

I have a very simple script in python that runs a user defined function (hetero) that joins sequences (strings of text) together over very large files, 2 sequences (rows) at a time. Anyway, As I have it written, it prints out to the screen, but I would like to write all output to a single file.

f = open ("new", "r")
while True:
    line1 = f.readline()
    line1a = line1.split()
    line2 = f.readline()
    line2a =line2.split()
    if not line2: break

       tri="".join ([hetero(b1, b2) for (b1, b2) in zip(line1a[2], line2a[2])])

       print line1a[1]+"_"+line1a[0],tri

This simply prints to the terminal the results of the script. So I tried to write the results (from the print command, "line1a[1]+.....") to an another file opened for writing (appended to the end of the script):

out_file = open ("out.txt", "w")
out_file.write(line1a[1]+"_"+line1a[0],tri)
out_file.close()

But of course it does not work. I don't understand why though...Do I need to open the file to write along with the file for reading, so that its outside teh While loop? The thing that is tricky is that the script reads in two lines at a time over the entire file, and prints the ID info and the sequence in a single line, each time -- I want to print all those results to a single file.

This is a simple fix I'm sure, but I don't use python that often and always find the file system i/o difficult to deal with.

2
  • If you don't like python file I/O :(, you could also use your shell to redirect console output to a file. Instead of python my_program.py do a python my_program.py > out.txt. This way you can keep your print statement as is. Commented Oct 9, 2015 at 15:11
  • Open the output file before the loop, write in the loop, close after the loop Commented Oct 9, 2015 at 15:12

4 Answers 4

3

Every time you open the file for writing it gets truncated. If you want to append, you can open it at the beginning and keep it open, or open in append mode instead (a instead of w).

Also, you should be using the with statement:

with open('new', 'r') as f, open('out.txt', 'w') as out:
    while True:
        ... 

That will call close automatically for you after the block ends.

You can also clean up your "read a pair of lines and split them" code. Instead of while True:

from itertools import izip
pairs = ((l1.split(), l2.split()) for l1, l2 in izip(f, f))
for line1a, line2a in pairs:
    tri = ...

Note that you want to use izip instead of zip or it'll just read the whole file into memory right away.

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

Comments

1

Not sure where you put your out_file code but you likely put that in the loop and it opened and closed every pass. Try something like

with open('out.txt', 'w') as outfile, open("new", "r") as f:
    while True:
        line1 = f.readline()
        line1a = line1.split()
        line2 = f.readline()
        line2a =line2.split()
        if not line2: break

           tri="".join ([hetero(b1, b2) for (b1, b2) in zip(line1a[2], line2a[2])])

           #print line1a[1]+"_"+line1a[0],tri
           out_file.write(line1a[1]+"_"+line1a[0],tri)

EDIT You'll notice I opened the file using a context, I am fan of this because you don't have to worry about closing it later and it seems clearer to me how long the file is open

Comments

0

You are using this code

out_file = open ("out.txt", "w")
out_file.write(line1a[1]+"_"+line1a[0],tri)
out_file.close()

at every iteration. Note the 'w' flag: this means you are opening again the file at each iteration and overwriting it from start. If you want instead to append to it you can use the flag 'a'.

But there is more: this code

out_file = open ("out.txt", "w")
[while ...]
out_file.close()

should be outside the while loop, since you only need to open and close this file once.

Comments

0

You can only open the file inside the loop if you open it like:

out_file = open ("out.txt", "a")

Notice the "a" for appending mode.

If you open it using "w" it will be overwritten every iteration of the loop.

You can check this Python files reference to learn more about.

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.