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
  • Write robust python code that take from the user the name of file to open. Then,...

    Write robust python code that take from the user the name of file to open. Then, take name from the user to search inside the file then display its phone number if found

  • Write a python program that prompts the user for the names of two text files and...

    Write a python program that prompts the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the scripts should simply output “Yes”. If they are not, the program should output “No”, followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file. The loop should break as soon as a...

  • FIRST: Write code that prompts the user for the name of a text file, opens that...

    FIRST: Write code that prompts the user for the name of a text file, opens that file if it exists and reads the file one line at a time printing each line to the screen. You must implement the file read in a try/catch block rather than throwing the exception on the main method. NEXT: Modify your code so that the program takes the name of two files from the command line, opens the first file if it exists for...

  • Write a Python program which prompts the user for a file name then a sentence. Display...

    Write a Python program which prompts the user for a file name then a sentence. Display the number of characters in the sentence and save the sentence to the file name. If the file already exists, ask the user if the file should be overwritten.

  • (Python Programming)Write a Python program that prompts the user to enter a list of words and...

    (Python Programming)Write a Python program that prompts the user to enter a list of words and stores in a list only those words whose first letter occurs again within the word (for example, 'Baboon'). The program should display the resulting list.

  • Program must be in Python 3. Write a program that prompts the user to enter two...

    Program must be in Python 3. Write a program that prompts the user to enter two strings and then displays a message indicating whether or not the first string starts with the second string. Your program must satisfy the following requirements. 1. Your program must include a function called myStartsWith that takes two string parameters. This function returns True if the rst string starts with the second string and returns False otherwise. 2. Your program must include a main function...

  • PYTHON 3.6.1 write a script named copyfile.py. this script should prompt the user for the names...

    PYTHON 3.6.1 write a script named copyfile.py. this script should prompt the user for the names of two text files. the contents of the first file should be input and written to the second file.

  • File Encryption and Decryption chapter 9 programming exercise #3 Design and write a python program to...

    File Encryption and Decryption chapter 9 programming exercise #3 Design and write a python program to successfully complete chapter 9 programming exercise #3. File Encryption and Decryption Write a program that uses a dictionary to assign “codes” to each letter of the alphabet. For example: codes = { ‘A’ : ‘%’, ‘a’ : ‘9’, ‘B’ : ‘@’, ‘b’ : ‘#’, etc . . .} Using this example, the letter A would be assigned the symbol %, the letter a would...

  • IN PYTHON Ask the user for a file name Open the requested file Split the contents...

    IN PYTHON Ask the user for a file name Open the requested file Split the contents of the file into words Iterate over each word Translate the word to Pig Latin Output the translated word to the screen How to Speak Pig Latin https://www.wikihow.com/Speak-Pig-Latin

  • write a python program that prompts the user for a name of a text file, opens...

    write a python program that prompts the user for a name of a text file, opens that file for reading , and tracks the unique words in a file and counts how many times they occur in the file. Your program should output the unique words and how often they occur in alphabetical order. Negative testing for a file that does not exist and an empty file should be implemented.

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