views:

102

answers:

1

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:
        with open("file3") as f3:
            with open("file4") as f4:
                    # Read one line from each 'in_' file
                    # Do many operations on the DNA sequences...
                    # Append one line to each 'out_' file

If yes, could I do this while avoiding the highly indented code (commented part, which may itself contain indented lines. Unless, as suggested, I use appropriately defined functions beforehand)? Thanks for the insights!

+4  A: 

contextlib.nested() allows you to chain multiple context managers in a single statement:

with contextlib.nested(open(...), open(...), ...) as (in_1, in_2, ...):
  ....
Ignacio Vazquez-Abrams
Wow, I learn something new every day.
Goose Bumper
@Goose Bumper: I've been using Python for 8 years now and I'm *still* learning new things.
Ignacio Vazquez-Abrams
My next step towards improving my Python is DEFINITELY reading the common libraries documentation! Thanks
Morlock