Working with multiple input and output files in Python

Posted by Morlock on Stack Overflow See other posts from Stack Overflow or by Morlock
Published on 2010-03-17T19:10:50Z Indexed on 2010/03/17 19:51 UTC
Read the original article Hit count: 272

Filed under:
|
|
|

I need to open multiple files (2 input and 2 output files), do complex manipulations on the lines from input files and then append results at the end of 2 output files. I am currently using the following approach:

in_1 = open(input_1)
in_2 = open(input_2)
out_1 = open(output_1, "w")
out_2 = open(output_2, "w")

# Read one line from each 'in_' file
# Do many operations on the DNA sequences included in the input files
# Append one line to each 'out_' file

in_1.close()
in_2.close()
out_1.close()
out_2.close()

The files are huge (each potentially approaching 1Go, that is why I am reading through these input files one at a time. I am guessing that this is not a very Pythonic way to do things. :) Would using the following form good?

with open("file1") as f1:
    with open("file2") as f2: # etc.

If yes, could I do it while avoiding the highly indented code that would result? Thanks for the insights!

© Stack Overflow or respective owner

Related posts about python

Related posts about best-practices