accepting file path or file object?

Peter Otten __peter__ at web.de
Mon Nov 5 06:49:01 EST 2012


andrea crotti wrote:

> Quite often I find convenient to get a filename or a file object as
> argument of a function, and do something as below:
> 
> def grep_file(regexp, filepath_obj):
>     """Check if the given text is found in any of the file lines, take
>     a path to a file or an opened file object
>     """
>     if isinstance(filepath_obj, basestring):
>         fobj = open(filepath_obj)
>     else:
>         fobj = filepath_obj
> 
>     for line in fobj:
>         if re.search(regexp, line):
>             return True
> 
>     return False
> 
> 
> This makes it also more convenient to unit-test, since I can just pass
> a StringIO.  But then there are other problems, for example if I pass
> a file object is the caller that has to make sure to close the file
> handle..
> 
> So I'm thinking if it's not just worth to skip the support for file
> objects and only use the filenames, which seems a more robust and
> consistent choice..
> 
> Any comment/suggestions about this?

I sometimes do something like this:

$ cat xopen.py
import re
import sys
from contextlib import contextmanager

@contextmanager
def xopen(file=None, mode="r"):
    if hasattr(file, "read"):
        yield file
    elif file == "-":
        if "w" in mode:
            yield sys.stdout
        else:
            yield sys.stdin
    else:
        with open(file, mode) as f:
            yield f

def grep(stream, regex):
    search = re.compile(regex).search
    return any(search(line) for line in stream)

if len(sys.argv) == 1:
    print grep(["alpha", "beta", "gamma"], "gamma")
else:
    with xopen(sys.argv[1]) as f:
        print grep(f, sys.argv[2])
$ python xopen.py 
True
$ echo 'alpha beta gamma' | python xopen.py - gamma
True
$ echo 'alpha beta gamma' | python xopen.py - delta
False
$ python xopen.py xopen.py context
True
$ python xopen.py xopen.py gamma
True
$ python xopen.py xopen.py delta
False
$ 





More information about the Python-list mailing list