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

Simon Mullis simon at mullis.co.uk
Thu Feb 5 14:09:17 CET 2009


Last try at getting the indenting to appear correctly..

#!/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 = 0
        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", help="""use this glob""")
    p.add_option("--stdin", dest="stdin", action="store_true",
default="False", help="""use stdin""")
    (options, args) = p.parse_args()
    main()



More information about the Python-list mailing list