Question

IN PYTHON Write a block of code that prompts the user for two filenames. Open the...

IN PYTHON Write a block of code that prompts the user for two filenames. Open the first file, display # its contents to the screen but redirect the content to the second file.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

file_1 = input("Enter the name of first file : ")
file_2 = input("Enter the name of second file : ")

fh1 = open(file_1,'r') # Open file 1 in read mode
fh2 = open(file_2,'a') # Open file 2 in append mode

for line in fh1.readlines(): # Traversing each line of file 1
    print(line,end = '') # Printing lines of first file
    fh2.write(line) # Appending second file
    
# Closing the file handlers
fh1.close()
fh2.close()

File1.txt

File2.txt

OUTPUT:

Appended File2.txt

Add a comment
Know the answer?
Add Answer to:
IN PYTHON Write a block of code that prompts the user for two filenames. Open the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT