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:
pythonfilename = 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:
- Prompts the user for a file name using the
input()
function and stores the result in thefilename
variable. - Tries to open the file with the given filename using the
open()
function and the'r'
mode. - Reads the contents of the file using the
read()
method and converts the contents to uppercase using theupper()
method. - Prints the uppercase contents to the console using the
print()
function. - Closes the file using the
close()
method. - 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.
Add a comment