From 20a18816e470b83115d6b3626dec37c6260b8517 Mon Sep 17 00:00:00 2001 From: Vaibhav Prasad <69839789+VaibhavPr@users.noreply.github.com> Date: Thu, 1 Jul 2021 11:51:49 +0530 Subject: [PATCH] Update wk7 - assignment 7.1.py This will work fine. Save words.txt preferably on your Desktop folder. --- wk7 - assignment 7.1.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/wk7 - assignment 7.1.py b/wk7 - assignment 7.1.py index 1ec76be..089fe08 100644 --- a/wk7 - assignment 7.1.py +++ b/wk7 - assignment 7.1.py @@ -1,12 +1,17 @@ 7.1 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. You can download the sample data at http://www.pythonlearn.com/code/words.txt -# Use words.txt as the file name -fname = raw_input("Enter file name: ") -fh = open(fname) + #Use words.txt as the file name +fname = input("Enter file name: ") +try : + fh = open(fname) -for line in fh: - line = line.rstrip() -line = line.upper() -print line +except: + print('Cannot open the file ',fname ,'please try again') + quit() + +content = fh.read() +content = content.upper() +content = content.rstrip() +print(content)