PY4E - Chapter 7练习题| Files|find等method使用|Easter Egg

如果对需要提取的文件提取路径有疑问,请看这篇⬇️

Jupyter Notebook 打开txt文件 - 存储路径和open语句_阿万的万的博客-CSDN博客

​​​​​​​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.py4e.com/code3/mbox-short.txt

fname = input('Please enter the file name:')
fhand = open(fname)

for line in fhand:
    line = line.upper()
    print(line)

运行结果:

Please enter the 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

Exercise 2: Write a program to prompt for a file name, and then read through the file and look for lines of the form:

X-DSPAM-Confidence: 0.8475

When you encounter a line that starts with “X-DSPAM-Confidence:” pull apart the line to extract the floating-point number on the line. Count these lines and then compute the total of the spam confidence values from these lines. When you reach the end of the file, print out the average spam confidence.

Enter the file name: mbox.txt
Average spam confidence: 0.894128046745

Enter the file name: mbox-short.txt
Average spam confidence: 0.750718518519

Test your file on the mbox.txt and mbox-short.txt files.

fname1 = input('Enter the file name: ')
fname = open(fname1)
count = 0
total = 0
for line in fname:
    if line.startswith('X-DSPAM-Confidence:'):
        count = count + 1
        po1 = line.find(':')
        po2 = line.find(' ',po1+2)
        num = line[po1+1:po2]
        num = float(num)
        total = num + total

ave = total / count
print('Average spam confidence:',ave)
运行结果一:

Enter the file name:  mbox.txt
Average spam confidence: 0.8941280467445736
运行结果二:
Enter the file name:  mbox-short.txt
Average spam confidence: 0.7507185185185187

Exercise 3: Sometimes when programmers get bored or want to have a bit of fun, they add a harmless Easter Egg to their program. Modify the program that prompts the user for the file name so that it prints a funny message when the user types in the exact file name “na na boo boo”. The program should behave normally for all other files which exist and don’t exist. Here is a sample execution of the program:

python egg.py
Enter the file name: mbox.txt
There were 1797 subject lines in mbox.txt

python egg.py
Enter the file name: missing.tyxt
File cannot be opened: missing.tyxt

python egg.py
Enter the file name: na na boo boo
NA NA BOO BOO TO YOU - You have been punk'd!

fname = input('Please enter the file name:')
try:
    fhand = open(fname)
    count = 0
    for line in fhand:
        if line.startswith('Subject:'):
            count = count + 1
    print('There were', count,'subject lines in', fname)

#🏺🏺🏺彩蛋部分🏺🏺🏺
except: 
    if fname1 == 'na na boo boo':
        print("NA NA BOO BOO TO YOU - You have been punk'd!")
    else:
        print('File cannot be opened:', fname)

运行结果:

Please enter the file name: mbox.txt
There were 1797 subject lines in mbox.txt
🏺Please enter the file name: na na boo boo🏺
🏺NA NA BOO BOO TO YOU - You have been punk'd!🏺

欢迎一起讨论!

如需转载请联系作者并注明出处。

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值