On Thu, Feb 5, 2009 at 2:58 AM, Simon Mullis <si...@mullis.co.uk> wrote:
> 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"

I'd add some print()s in the above loop (and also the 'for f in files'
loop) to make sure the part of the code you didn't want to share ("do
stuff with the line") works correctly, and that nothing is improperly
looping in some unexpected way.

Also, there are several series of lines with invalid indentation;
could be an email artifact or could be the cause of your problem. If
the print()s don't yield any useful insight, repost the code again
with absolutely correct indentation.

Finally, some stylistic points:
- don't do 'if (foo):'   use the less noisy 'if foo:' instead
- don't do 'i = int()'   use the more obvious 'i = 0' instead

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to