Created
May 17, 2010 10:37
-
-
Save apg/403626 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""ARGF from Ruby in Python. | |
""" | |
import sys, os | |
class _ARGF(object): | |
def __init__(self): | |
self.lineno = 1 | |
self.file = None | |
def __iter__(self): | |
return self.next() | |
def next(self): | |
files = filter(os.path.isfile, sys.argv) | |
if files: | |
for f in files: | |
self.lineno = 0 | |
self.file = f | |
for n in open(f).xreadlines(): | |
self.lineno += 1 | |
yield n | |
else: | |
for n in sys.stdin.readlines(): | |
self.file = 'STDIN' | |
self.lineno += 1 | |
yield n | |
ARGF = _ARGF() | |
if __name__ == '__main__': | |
for n in ARGF: | |
print ARGF.file, ":", ARGF.lineno, ':', n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment