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

Simon Mullis simon at mullis.co.uk
Thu Feb 5 05:58:48 EST 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
filehandles.

>>> 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 test_fh.py

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

    def scan_data(self,fileobject):
                i = int()
        for line in fileobject:
                        print i
                        i += 1
                        # do stuff with the line...
                        pass
               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",
default="False")
        (options, args) = p.parse_args()
        main()

#EOF

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

# cat test_file | ./test-fh.py --stdin
...output...
# echo $?
0

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

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

So, what am I doing wrong?

Thanks in advance

SM

-- 
Simon Mullis
_________________
simon at mullis.co.uk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090205/dcb5ec32/attachment.html>


More information about the Python-list mailing list