Is there a more pythonic way to open a file if given one as an argument or stdin if not?

command-line, python, unix

Solution

The fileinput module is perfect for this.

Problem

I'm trying to write a python script which follows the common unix command line pattern of accepting input from stdin if no file name is given. This is what I've been using: ``` if __name__ == "__main__": if len(sys.argv) > 1: stream = open(sys.argv[1]) else: stream = sys.stdin ``` Is there a more pythonic way to do that?

Original source

Related problems