Mar-24-2017, 06:44 AM
i wanted to come up with a bigger script that made use of the builtin zip() function. this script is a command to interleave the lines of 2 or more files.
!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
"""Interleave 2 or more files, line by line.
file interleavefiles.py
command interleavefiles
purpose interleave 2 or more files, line by line
author Phil D. Howard
email 10054452614123394844460370234029112340408691
The intent is that this command works correctly under both Python 2 and
Python 3. Please report failures or code improvement to the author.
"""
__license__ = """
Copyright (C) 2017, by Phil D. Howard - all other rights reserved
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
The author may be contacted by decoding the number
10054452614123394844460370234029112340408691
(provu igi la numeron al duuma)
"""
from sys import argv, stderr, stdout, version_info
def main(args):
"""main"""
errors=0
openfiles=[]
for filename in args[1:]:
try:
openfile=open(filename,'r')
openfiles.append(openfile)
except IOError:
print('unable to open file',repr(filename),file=stderr)
errors+=1
for lines in zip(*openfiles):
for line in lines:
print(line.rstrip())
for openfile in openfiles:
openfile.close()
return 0
if __name__ == '__main__':
try:
result=main(argv)
stdout.flush()
except KeyboardInterrupt:
print('')
reselt=99
except IOError:
result=98
if result in (None,True,):
exit(0)
if result in (False,):
exit(1)
if isinstance(result,(str,bytes,bytearray) if version_info.major>2 else (str,unicode)):
print(result,file=stderr)
exit(1)
try:
exit(int(result))
except ValueError:
print(str(result),file=stderr)
except TypeError:
exit(0 if result == None else 255)
# EOF
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
