Changeset 36:f6e3e798fa2a in misc for tictac.py
- Timestamp:
- 09/02/07 00:29:48 (5 years ago)
- Branch:
- misc
- Convert:
- svn:e60e002c-0983-44b9-b2ae-8842d539f768/misc@39
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tictac.py
r35 r36 24 24 ## 25 25 26 import atexit 26 27 import os.path 28 import StringIO 27 29 28 30 from optparse import OptionParser 29 from sys import std out31 from sys import stdin, stdout 30 32 31 33 def backread(afile, buffer_size=1024): … … 45 47 yield ''.join(reversed(data)) 46 48 49 def _print(data): 50 for l in backread(data): 51 stdout.write(l) 52 stdout.write('\n') 53 47 54 def main(): 48 usage = 'usage: %prog file' 55 interactive = False 56 data = StringIO.StringIO() 57 usage = '''usage: %prog [FILE] 58 59 With no FILE read standard input 60 ''' 49 61 parser = OptionParser(usage) 50 62 (options, args) = parser.parse_args() 51 63 52 if len(args) !=1:53 parser.error('Incorrect number of arguments')64 if len(args) < 1: 65 interactive = True 54 66 elif not os.path.isfile(args[0]): 55 67 parser.error('No such file') 56 68 57 fin = open(args[0], 'r') 58 try: 59 for l in backread(fin): 60 stdout.write(l) 61 stdout.write('\n') 62 finally: 63 fin.close() 69 if interactive: 70 while True: 71 line = stdin.readline() 72 if not line: break 73 data.write(line) 74 atexit.register(_print, data) 75 else: 76 fin = open(args[0], 'r') 77 try: 78 _print(fin) 79 finally: 80 fin.close() 64 81 65 82 if __name__ == '__main__':
Note: See TracChangeset
for help on using the changeset viewer.
