Question: Write a program that creates of list of input file names and then concatenates the contents of these input files into one file. The following

Write a program that creates of list of input file names and then concatenates the contents of these input files into one file. The following example shows the concatenation of two files, but the program should be able to handle additional files. You can download the files below for testing in an IDE.
Write the following functions:
def getFiles()- that creates a list and prompts the user to enter file names (.txt format) and a 'blank line to quit'. Use a while loop to get the file names and append them to the list. Return the list from the function (see 6.3 Appending Lists)
def getOutFile()- that gets the name of output file (.txt format) and returns the file name from the function. Simple as that: prompt for the name and return from function.
def processFiles(files, out_file)- that takes 2 parameters for the list and the output file name. Use a for loop to traverse the list, a variable to open each file, read it, and write it to the out_file. No return value needed. Close the input file (not the out_file) you created for reading.
def main()- that calls the functions and processes the files (written for you).
Ex: If the input file mary.txt contains:
Mary had a little lamb
Whose fleece was white as snow.
And everywhere that Mary went,
The lamb was sure to go!
and the input file frost.txt contains:
Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.
then the output file append.txt will contain:
Mary had a little lamb
Whose fleece was white as snow.
And everywhere that Mary went,
The lamb was sure to go!
Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.
A run of the program is:
# Concatenate several input files into a single output file.
#
def main() :
listoffiles = getFiles ()
out_filename = getoutFile()
# Open the output file.
outf = open(out_filename, "w")
processfiles(listoffiles, outf)
outf.close()
# Function to create a list to hold input file names and fill the list (while loop)
# Function to create and open the output file
# Function to process the input files to concatenate (for loop)
# Call main function
main()Enter input file name (blank line to quit): mary.txt
Enter input file name (blank line to quit): frost.txt
Enter input file name (blank line to quit):
Enter output file name: append.txt
 Write a program that creates of list of input file names

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!