File: class/Workbook/Exercises/Lab8/unpack2.py

#!/usr/local/bin/python
import sys
marker = ':'*6

def unpack_file(file):
    for line in file.readlines():         # for all input lines
        if line[:6] != marker:
            print line,                   # write real lines
        else:
            sys.stdout = open(line[6:-1], 'w')

if __name__ == '__main__':
    if len(sys.argv) == 1 or sys.argv[1] == '-':
        unpack_file(sys.stdin)                     # from stdin stream
    else:
        unpack_file(open(sys.argv[1], 'r'))        # passed in filename



[Home page] Books Code Blog Python Author Train Find ©M.Lutz