Linux cat in python (ex5)

Hi there. Here is my implementation i was able to create in 45 min
‘>’ sign is equal to --T (which should mean ‘to file’)

import argparse
parser = argparse.ArgumentParser()


parser.add_argument('files', metavar='file', type=str
, nargs = '*')
parser.add_argument('--T', action='store', required=False)


args = parser.parse_args()


cont = []

for f in args.files:
    with open(f,mode='r') as f:
        content = f.read()
        cont.append(content)
        print(content, end='')

if args.T:
    with open(args.T, mode='w') as f:
        l = ''.join(cont)
        f.write(l)

https://github.com/Murtagy you can communicate with me here, if you want! I am going to go through more python tutorial and later develop my Django-crm web-app

1 Like

Hi @Murtagy nice implementation and cool idea to showcase your solution. I’ll take the chance and post my hack too.

Hey, good idea with the CRM. I’m impressed with your plan.

import argparse

parser = argparse.ArgumentParser(description='Python cat emulator')
parser.add_argument('filename', nargs="+", type=argparse.FileType('r'), help='One or more files')
parser.add_argument('-o', '--outputfile', nargs="?", help="The name of the outputfile")
args = parser.parse_args()

def read_file():
    output = ""
    for i in args.filename:
        output += i.read()
    return output

if args.outputfile is not None:
    for i in args.filename:
        with open(args.outputfile, "a") as write_to_file:
            write_to_file.write(read_file())
else:
    print(read_file())