美文网首页
2017/2/28 files&strings

2017/2/28 files&strings

作者: 终焉的灰烬 | 来源:发表于2017-02-28 01:58 被阅读0次

    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())
    

    相关文章

      网友评论

          本文标题:2017/2/28 files&strings

          本文链接:https://www.haomeiwen.com/subject/gzntgttx.html