Ask Question
1 January, 23:36

Write a program that asks the user for the names of two files. The first file should be opened for reading and the second file should be opened for writing. The program should read the contents of the first file, change all characters to uppercase, and store the results in the second file. The second file will be a copy of the first file, except that all the characters will be uppercase. Use Notepad or another text editor to create a simple file that can be used to test the program.

+2
Answers (1)
  1. 2 January, 00:43
    0
    The solution code is written in Python 3

    1. output = ""

    2. with open ("text1. txt") as file:

    3. data = file. readlines ()

    4.

    5. for r in dа ta:

    6. output + = r. upper ()

    7.

    8. with open ("text2. txt", "w") as file:

    9. file. write (output)

    Explanation:

    Firstly, let's ready a variable output to hold the read data from the first text file (Line 1).

    Next, use open function to create a file stream object and use its readlines () method to read all rows of data from text1 (Line 2 - 3)

    Next create a for loop to traverse through every row of the read data and use upper () function to change all characters in the current row to uppercase and append it to output variable.

    Once the entire output string is ready, use open function again to create a file stream object but add "w" as second parameter of the open function (Line 8).

    Lastly, use write method to copy the uppercase text held by the output variable, to the new file, text2 (Line 9).
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program that asks the user for the names of two files. The first file should be opened for reading and the second file should be ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers