2017/2/28 files&strings

Exercise 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.pythonlearn.com/code3/mbox-short.txt

The answer:

file_name = input('Enter the file name:')
try:
    fhand = open(file_name)
except:
    print('File cannot be opened:', file_name)
    exit()
for line in fhand:
    if line == '\n':
        continue
    line = line.strip()
    print(line.upper())
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,081评论 0 23
  • 从前有座山,山上有座庙,庙里有个老和尚。 呸。 许小青每次下山总是听娃娃们唱着这歌谣,他捏着糖葫芦,幻了娃娃模样,...
    言司so阅读 260评论 0 1
  • 故事从松子成人后的经历开始。 24岁 音乐老师,因为偷窃被学校开除;离家出走,与家里面断绝关系与一名具有太宰治气质...
    拍谢少女阅读 1,043评论 9 49
  • 新学期第一个周末,我就过的热火朝天,可谓是忙的不可开交,一刻都没停息过。原本按惯例我周末都没有排班的,早上要...
    珍珍_9761阅读 218评论 0 0
  • 亦步亦趨的模仿 成為你想要的模樣 至於我本人的感受 反正無人在乎 請不要關注 Photo by Dilipsinh...
    憨憨爹阅读 193评论 0 0