Copy contents of one file to another fileWrite a Python program that copies the content from one file to another file.Note: Take the input file name from the user.Aim:To create a Python program that reads a text file, copies its contents to another file, and prints the contents to the console.Algorithm:Step 1: Accept a filename input from the user and store it in the variable file.Step 2: Open the input file (file) in read mode and the output file ('OutputData3.txt') in write mode using the open() function.Step 3: Iterate through each line in the input file (fin).Write each line to the output file (fout).Step 4: Close both the input and output files using the close() method.Step 5: Open the output file ('OutputData3.txt') in read mode and store it in the variable fin.Step 6: Iterate through each line in the output file (fin).Print each line to the console.Step 7: Close the output file (fin).Step 8: End the program.Sample Test CasesTest Case 1:Expected Output:Enter·file·name:·InputData1.txtThis·is·this.That·is·this.·That·is·that.·This·is·That.Now·tell·me·what·is·that?And·what·is·this?Confused?Haha!!Test Case 2:Expected Output:Enter·file·name:·InputData2.txtIn·a·coastal·town,·a·lighthouse·stood·tall,holding·a·magical·secret.·The·lighthousewhispered·stories·to·the·sea,·guidinglost·ships·and·providing·comfort·toweary·sailors.
Question
Copy contents of one file to another fileWrite a Python program that copies the content from one file to another file.Note: Take the input file name from the user.Aim:To create a Python program that reads a text file, copies its contents to another file, and prints the contents to the console.Algorithm:Step 1: Accept a filename input from the user and store it in the variable file.Step 2: Open the input file (file) in read mode and the output file ('OutputData3.txt') in write mode using the open() function.Step 3: Iterate through each line in the input file (fin).Write each line to the output file (fout).Step 4: Close both the input and output files using the close() method.Step 5: Open the output file ('OutputData3.txt') in read mode and store it in the variable fin.Step 6: Iterate through each line in the output file (fin).Print each line to the console.Step 7: Close the output file (fin).Step 8: End the program.Sample Test CasesTest Case 1:Expected Output:Enter·file·name:·InputData1.txtThis·is·this.That·is·this.·That·is·that.·This·is·That.Now·tell·me·what·is·that?And·what·is·this?Confused?Haha!!Test Case 2:Expected Output:Enter·file·name:·InputData2.txtIn·a·coastal·town,·a·lighthouse·stood·tall,holding·a·magical·secret.·The·lighthousewhispered·stories·to·the·sea,·guidinglost·ships·and·providing·comfort·toweary·sailors.
Solution
Here is a Python program that follows the algorithm you provided:
# Step 1: Accept a filename input from the user and store it in the variable file.
file = input("Enter file name: ")
# Step 2: Open the input file (file) in read mode and the output file ('OutputData3.txt') in write mode using the open() function.
with open(file, 'r') as fin, open('OutputData3.txt', 'w') as fout:
# Step 3: Iterate through each line in the input file (fin). Write each line to the output file (fout).
for line in fin:
fout.write(line)
# Step 4: Close both the input and output files using the close() method.
# This is done automatically when using 'with' statement
# Step 5: Open the output file ('OutputData3.txt') in read mode and store it in the variable fin.
with open('OutputData3.txt', 'r') as fin:
# Step 6: Iterate through each line in the output file (fin). Print each line to the console.
for line in fin:
print(line, end='')
# Step 7: Close the output file (fin).
# This is done automatically when using 'with' statement
# Step 8: End the program.
This program will copy the contents of the input file to 'OutputData3.txt' and then print the contents of 'OutputData3.txt' to the console.
Similar Questions
How do you write data to a file in Python?Question 10Answera.file.read()b.file.append(data)c.file.write(data)d.file.close()
Write a program to copy contents of file ABC.txt to file XYZ.txt.
python programs to create, read, write, append and delete a file
Write a Python program to read a file line by line store it into an array.
What method is used to read the entire content of a file in Python?Question 7Answera.file.write()b.file.append()c.file.read()d.file.close()
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.