Let's say I want to open a text file for reading using the following syntax:
with open(fname,'r') as f:
# do something
pass
But if I detect that it ends with .gz, I would call gzip.open().
if fname.endswith('.gz'):
with gzip.open(fname,'rt') as f:
# do something
pass
else:
with open(fname,'r') as f:
# do something
pass
If "do something" part is long and not convenient to write in a function (e.g. it would create a nested function, which cannot be serialized), what is the shortest way to call with either gzip.open or open based on the return of fname.endswith('.gz')?
An arbitrary number of files can be opened with the open () method supported in Python 2.7 version or greater. The following syntax is used to open multiple files : Different names are provided to different files.
The first parameter of the open () function is file, the absolute or relative path to the file that you are trying to work with. We usually use a relative path, which indicates where the file is located relative to the location of the script (Python file) that is calling the open () function. For example, the path in this function call:
The Python programming language has various functions and statements for working with a file. The with statement and open () function are two of those statements and functions. In this article, you will learn how to use both the with statement and open () function to work with files in Python. What Does Open () Do in Python?
To work with files in Python, you have to open the file first. So, the open () function does what the name implies – it opens a file for you so you can work with the file. To use the open function, you declare a variable for it first. The open () function takes up to 3 parameters – the filename, the mode, and the encoding.
The context manager helps close the object.
You don't have to create the object used as a context manager, at the same time you use with
to enter the context, though. The open()
and gzip.open()
calls return a new object that happens to be a context manager, and you can create them before you enter the context:
if fname.endswith('.gz'):
f = gzip.open(fname,'rt')
else:
f = open(fname, 'r')
with f:
# do something
In both cases, the object returns self
on entering the context, so there is no need to use as f
here.
Also, functions are first-class citizens, so you can also use a variable to store the function and then call that in the with
statement to create the context manager and file object:
if fname.endswith('.gz'):
opener = gzip.open
else:
opener = open
with opener(fname, 'rt') as f: # yes, both open and gzip.open support mode='rt'
# do something
This doesn't really buy you anything over the other method here, but you could use a dictionary to map extensions to callables if you so desire.
The bottom line is that with
calls context-manager hook methods, nothing less, nothing more. The expression after with
is supposed to supply such a manager, but creating that object is not subject to the context management protocol.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With