Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python3.2
- # -*- coding: utf-8 -*-
- # main.py
- """ Task: Exercise 7.1
- Write a program to read through a file and print the contents of the file
- (line by line) all in upper case. Executing the program will look as follows:
- python shout.py
- Enter a file name: mbox-short.txt
- FROM STEPHEN.MARQUARD@UCT.AC.ZA SAT JAN 5 09:14:16 2008
- RETURN-PATH: <POSTMASTER@COLLAB.SAKAIPROJECT.ORG>
- RECEIVED: FROM MURDER (MAIL.UMICH.EDU [141.211.14.90])
- BY FRANKENSTEIN.MAIL.UMICH.EDU (CYRUS V2.3.8) WITH LMTPA;
- SAT, 05 JAN 2008 09:14:16 -0500
- You can download the file from www.py4inf.com/code/mbox-short.txt
- """
- # Functions
- def enterFileName():
- """
- The user has to enter a filename.
- Returns fileName
- """
- fileName = None
- while fileName == None:
- # Enter filename
- try:
- fileName = input("Enter the filename: ")
- except:
- print("Invalid input!")
- continue
- return fileName
- # Main
- file = None
- fileName = enterFileName()
- try:
- file = open(fileName, 'r') # Open file
- except:
- print("File cannot be opened: " + str(fileName))
- exit()
- if file != None:
- try:
- for line in file: # Read file content
- print(line.rstrip().upper()) # Print content
- except:
- print("Error on reading file content!")
- try:
- file.close() # Close file
- except:
- print("File could not be closed!")
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement