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

<br>And I have no idea why... ;-)<br><br>sys.stdin and a normal file opened with "open" seem to both be identical filehandles.<br><br>>>> import sys<br>>>> foo = sys.stdin<br>>>> type(foo)<br>
<type 'file'><br>>>> repr(foo)<br>"<open file '<stdin>', mode 'r' at 0x16020>"<br><br>>>> bar = open("test_file", 'r')<br>>>> type(bar)<br>
<type 'file'><br>>>> repr(bar)<br>"<open file 'test_file', mode 'r' at 0x3936e0>"<br><br>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)<br>
<br>Code that illustrates the issue follows:<br><br># cat test_fh.py<br><br>#!/usr/bin/env python<br>import glob, os, sys<br>class TestParse(object):<br>    def __init__(self):<br>        if (options.stdin):<br>              self.scan_data(sys.stdin)<br>
        if (options.glob):<br>              self.files = glob.glob(options.glob)<br>                      for f in files:<br>                              fh = open(f, 'r')<br>                              self.scan_data(fh)<br>
                              fh.close()<br><br>    def scan_data(self,fileobject):<br>                i = int()<br>        for line in fileobject:<br>                        print i<br>                        i += 1<br>                        # do stuff with the line...<br>
                        pass<br>               print "finished file"<br><br>def main():<br>    T = TestParse()<br><br>if __name__ == "__main__":<br>    from optparse import OptionParser<br>        p = OptionParser(__doc__, version="testing 1 2 3")<br>
    p.add_option("--glob", dest="glob")<br>        p.add_option("--stdin", dest="stdin", action="store_true", default="False")<br>        (options, args) = p.parse_args()<br>
        main()<br><br>#EOF<br><br>Running this against stdin outputs a count of lines and then exits fine (exit code 0).<br>
<br># cat test_file | ./test-fh.py --stdin<br>...output...<br># echo $?<br>0<br><br>Running against --glob "test_file" just hangs.....<br><br># ./test_fh.py --glob "test_file"<br>....wait 20 seconds or so...<br>
^CTraceback (most recent call last):<br>  File "./test_fh.py", line 35, in <module><br>    main()<br>  File "./test_fh.py", line 26, in main<br>    T = TestParse()<br>  File "./test_fh.py", line 8, in __init__<br>
    self.scan_data(sys.stdin)<br>  File "./test_fh.py", line 18, in scan_data<br>    for line in fileobject:<br>KeyboardInterrupt<br> # echo $?<br>1<br><br>So, what am I doing wrong?<br><br>Thanks in advance<br>
<br>SM<br><br>-- <br>Simon Mullis<br>_________________<br><a href="mailto:simon@mullis.co.uk">simon@mullis.co.uk</a><br>