*args, **kwds)[source]

Context manager, like open, but lets file handles and file like objects pass untouched.

It is useful when implementing a function that can accept both strings and file-like objects (like numpy.loadtxt, etc).


filepath_or : str/bytes/unicode string or file-like

If string, file to be opened using open. Else, it is returned untouched.

Other Parameters:

args, kwargs : tuple, dict

When filepath_or is a string, any extra arguments are passed on to the open builtin.


>>> with open_file('filename') as f:  
...     pass
>>> fh = open('filename')             
>>> with open_file(fh) as f:          
...     pass
>>> fh.closed                         
>>> fh.close()