Skip to content

Instantly share code, notes, and snippets.

@apg
Created May 17, 2010 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apg/403626 to your computer and use it in GitHub Desktop.
Save apg/403626 to your computer and use it in GitHub Desktop.
"""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