Python function that accepts file object or path

Viewed 8389

I want to write a function that accepts either a path as a string or a file object. So far I have:

def awesome_parse(path_or_file):
    if isinstance(path_or_file, basestring):
        f = open(path_or_file, 'rb')
    else:
        f = path_or_file
    with f as f:
        return do_stuff(f)

where do_stuff takes an open file object.

Is there a better way to do this? Does with f as f: have any repercussions?

Thanks!

2 Answers
Related