write a program that prompts for a file name then opens that file and reads through the file and print the contents of the file in upper case. use the file words.txt to produce the output below

Hi, in this article let's Learn how to open a file  in the read mode, read all the content of the file and print the contents in uppercase letters.  below is a Python program statement

 

write a program that prompts for a file name, then opens that file and reads through the file, and print the contents of the file in upper case. use the file words.txt to produce the output below

 




Write a Program That Prompts For a File Name, Then Opens That File And Reads Through The File, And Print The Contents of The File in Upper Case. Use The File words.txt to Produce The Output


Lets use the input function to ask a user to enter the filename that is words.txt.  Store the file name into a variable. Take another variable known as file descriptor read and initialize to none.

filename = input("Enter a filename : ")
fd_read = None




Then use the try block to to open the file in the read mode.  if the filename is not present then except file not found error,  and exit the program

try:
   fd_read = open(filename, "r")
except FileNotFoundError:
   print("The file you entered does not exists")
   exit(1) 

 



if the file is designed with an using the file descriptor read all the lines present in the file using the  for loop and convert the the line to uppercase and print it.  finally close the file using close function call

for line in fd_read.readlines():
   print(line.upper(), end="")

fd_read.close()




Final program


filename = input("Enter a filename : ")
fd_read = None

try:
   fd_read = open(filename, "r")
except FileNotFoundError:
   print("The file you entered does not exists")
   exit(1)

for line in fd_read.readlines():
   print(line.upper(), end="")

fd_read.close()

 


Input:  contents of  words.txt

Write a Program That Prompts For a File Name
Then Opens That File And Reads Through The File,
And Print The Contents of The File in Upper Case.
Use The File words.txt to Produce The Output



Output:

Enter a filename : words.txt
WRITE A PROGRAM THAT PROMPTS FOR A FILE NAME
THEN OPENS THAT FILE AND READS THROUGH THE FILE,
AND PRINT THE CONTENTS OF THE FILE IN UPPER CASE.
USE THE FILE WORDS.TXT TO PRODUCE THE OUTPUT




Conclusion: Run the program by yourself by adding some more extra lines into the words.txt and run the program and get the output in uppercase.

Post a Comment

1 Comments

  1. fname = input("Enter file name: ")

    try:
    fhand=open(fname)
    except:
    print("File name not exists")
    quit()
    for line in fhand:
    line.rstrip()
    print(line.upper(),end='')

    ReplyDelete