Should "open(sys.stdin)" and "open(file, 'r')" be equivalent?

Simon Mullis simon at
Thu Feb 5 11:58:48 CET 2009

Hi All

I've written a simple python script that accepts both stdin and a glob (or
at least, that is the plan).
Unfortunately, the glob part seems to hang when it's looped through to the
end of the filehandle.

And I have no idea why... ;-)

sys.stdin and a normal file opened with "open" seem to both be identical

>>> import sys
>>> foo = sys.stdin
>>> type(foo)
<type 'file'>
>>> repr(foo)
"<open file '<stdin>', mode 'r' at 0x16020>"

>>> bar = open("test_file", 'r')
>>> type(bar)
<type 'file'>
>>> repr(bar)
"<open file 'test_file', mode 'r' at 0x3936e0>"

The stdin version is fine. I want to re-use the code for scan_data (and all
of the other processing methods) so I'd like to be able to iterate over one
line at a time, independently of the source (i.e. either file or stdin)

Code that illustrates the issue follows:

# cat

#!/usr/bin/env python
import glob, os, sys
class TestParse(object):
    def __init__(self):
        if (options.stdin):
        if (options.glob):
              self.files = glob.glob(options.glob)
                      for f in files:
                              fh = open(f, 'r')

    def scan_data(self,fileobject):
                i = int()
        for line in fileobject:
                        print i
                        i += 1
                        # do stuff with the line...
               print "finished file"

def main():
    T = TestParse()

if __name__ == "__main__":
    from optparse import OptionParser
        p = OptionParser(__doc__, version="testing 1 2 3")
    p.add_option("--glob", dest="glob")
        p.add_option("--stdin", dest="stdin", action="store_true",
        (options, args) = p.parse_args()


Running this against stdin outputs a count of lines and then exits fine
(exit code 0).

# cat test_file | ./ --stdin
# echo $?

Running against --glob "test_file" just hangs.....

# ./ --glob "test_file"
....wait 20 seconds or so...
^CTraceback (most recent call last):
  File "./", line 35, in <module>
  File "./", line 26, in main
    T = TestParse()
  File "./", line 8, in __init__
  File "./", line 18, in scan_data
    for line in fileobject:
 # echo $?

So, what am I doing wrong?

Thanks in advance


Simon Mullis
simon at
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list