Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the code so it runs in Python: Write a program that concatenates the co

ID: 3674734 • Letter: C

Question

Complete the code so it runs in Python:
Write a program that concatenates the contents of several files into one file. For example, $ python catfiles.py chapter1.txt chapter2.txt chapter3.txt book.txt makes a long file book.txt that contains the contents of the files chapter1.txt, chapter2.txt, and chapter3.txt. The target file is always the last file specified on the command line. The following code demonstrates simple command line argument passing:

# Write a program that concatenates the contents of several files into one file. # For example, # catfiles chapter1.txt chapter2.txt chapter3.txt book.txt # makes a long file book.txt that contains the contents of the files # chapter1.txt, chapter2.txt, and chapter3.txt. # The target file is always the last file specified on the command line. # IMPORTS from sys import argv # FUNCTIONS def get_arguments(): list_arguments = [] for arg in argv: list_arguments.append(arg) return list_arguments def catfile(input, output): output_file = open(output, "a") with open(input, "r") as file: for line in file: output_file.write(line) file.close() output_file.close() def catfiles(arguments_list, output_file): for i in range(1, len(arguments_list) - 1): catfile(arguments_list[i], output_file) # main def main(): arguments = get_arguments() output_file = arguments[-1] catfiles(arguments, output_file) # PROGRAM RUN if __name__ == "__main__": main()

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

def get_arguments():

char arg[4],argv;

List_arguments=[book.txt,chapter1.txt, chapter2.txt,chapter3.txt]

for arg in argv:

arg=raw_input("Enter the arguments:")

List_arguments.append(arg)

print "book.txt contains chapter1.txt,chapter2.txt,chapter3.txt"

def catfile(chapter1.txt,book.txt)

book.txt=open(book.txt,"a")

with open (chapter1,"r") as file

for line in file

book.txt.write(line)

file.close()

book.txt.close()

def catfile(chapter2.txt,book.txt)

book.txt=open(book.txt,"a")

with open(chapter2.txt,"r") as file

for line in file

book.txt.write(line)

file.close()

book.txt.close()

def catfile(chapter3.txt,book.txt)

book.txt=open(book.txt,"a")

with open(chapter3.txt,"r") as file

for line in file

book.txt.write(line)

file.close()

book.txt.close()

def catfile(arguments_list, book.txt):

for i in rang(1,len(arguments_list)-1):

i=raw_input("Enter the value of i:")

catfile(arguments_list[i], book.txt)

void main():

arguments=get_arguments()

book.txt=arguments(-1)

catfiles(arguments,book.txt)

if(book.txt=chapter1.txt_main()):

def catfile(chapter1.txt,book.txt)

elif(book.txt=chapter2.txt_main()):

def catfile(chapter2.txt,book.txt)

else

def catfile(chapter3.txt,book.txt)