PEP 308: A PEP Writer's Experience - PRO

Fredrik Lundh fredrik at pythonware.com
Mon Feb 10 10:10:24 EST 2003


Andrew Dalke wrote:

> Actually, you still need to open the file, correct?  If you use
> the if/else above then you need to check the extension twice,
> which seems more fragile in the face of maintainence.  Instead,
> it should be
>
>   if filename == "-":
>     displayname = "<stdin>"
>     infile = sys.stdin
>   else:
>     displayname = filename
>     infile = open(filename)
>   ....
>   print "Processing", displayname

or, shorter:

    if filename == "-":
        infile = sys.stdin
    else:
        infile = open(filename)
    ...
    print "Processing", infile.name

</F>








More information about the Python-list mailing list