I think the most Pythonic approach to this particular file problem is to use the fileinput module (since you either need complex context managers or error handling with open ), I'm going to start with the Ashwini example, but add a few things. Firstly, itβs better to open the U flag to support Universal Newlines (assuming your Python is compiled with it, and most of them are), ( r is the default mode, but explicit is better than implicit). If you work with other people, it is best to support them by giving you files in any format.
import fileinput for line in fileinput.input(['file1', 'file2'], mode='rU'): pass
This can also be used on the command line, as it will accept sys.argv [1:] if you do this:
import fileinput for line in fileinput.input(mode='rU'): pass
And you will transfer the files in your shell as follows:
$ python myscript.py file1 file2
Aaron Hall Feb 17 '14 at 19:01 2014-02-17 19:01
source share