Advertisement
Mars83

7-1

Oct 7th, 2011
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. #! /usr/bin/env python3.2
  2. # -*- coding: utf-8 -*-
  3.  
  4. # main.py
  5. """ Task: Exercise 7.1
  6.    Write a program to read through a file and print the contents of the file
  7.    (line by line) all in upper case. Executing the program will look as follows:
  8.    python shout.py
  9.    Enter a file name: mbox-short.txt
  10.    FROM STEPHEN.MARQUARD@UCT.AC.ZA SAT JAN 5 09:14:16 2008
  11.    RETURN-PATH: <POSTMASTER@COLLAB.SAKAIPROJECT.ORG>
  12.    RECEIVED: FROM MURDER (MAIL.UMICH.EDU [141.211.14.90])
  13.    BY FRANKENSTEIN.MAIL.UMICH.EDU (CYRUS V2.3.8) WITH LMTPA;
  14.    SAT, 05 JAN 2008 09:14:16 -0500
  15.    You can download the file from www.py4inf.com/code/mbox-short.txt
  16. """
  17.  
  18. # Functions
  19. def enterFileName():
  20.     """
  21.    The user has to enter a filename.
  22.    Returns fileName
  23.    """
  24.     fileName = None
  25.     while fileName == None:
  26.         # Enter filename
  27.         try:
  28.             fileName = input("Enter the filename: ")
  29.         except:
  30.             print("Invalid input!")
  31.             continue
  32.     return fileName
  33.  
  34. # Main
  35. file = None
  36. fileName = enterFileName()
  37. try:
  38.     file = open(fileName, 'r')      # Open file
  39. except:
  40.     print("File cannot be opened: " + str(fileName))
  41.     exit()
  42. if file != None:
  43.     try:
  44.         for line in file:           # Read file content
  45.             print(line.rstrip().upper())    # Print content
  46.     except:
  47.         print("Error on reading file content!")
  48.     try:
  49.         file.close()                # Close file
  50.     except:
  51.         print("File could not be closed!")
  52.         exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement