Prompt Title: Learning Programming with AI

Created 1 year ago
Votes 0
Views 52 times
IT
0

Prompt Detail:

Sure, here's a Python program that prompts the user for a file name, opens the file, reads its contents, and prints the contents in uppercase letters:

python
filename = input("Enter a file name: ") try: file = open(filename, 'r') contents = file.read().upper() print(contents) file.close() except FileNotFoundError: print("File not found.")

Here's what this program does:

  1. Prompts the user for a file name using the input() function and stores the result in the filename variable.
  2. Tries to open the file with the given filename using the open() function and the 'r' mode.
  3. Reads the contents of the file using the read() method and converts the contents to uppercase using the upper() method.
  4. Prints the uppercase contents to the console using the print() function.
  5. Closes the file using the close() method.
  6. If the file is not found, prints an error message using the print() function.

Assuming you have a file called words.txt in the same directory as the Python program, you can enter "words.txt" when prompted for the file name to get the desired output.

avatar
LichKolia
Shared 1 prompt
Created 1 year ago

Leave a Comment