Python编程从入门到实践 第十章-文件和异常

10-1

with open('wt_canido.txt') as file_a :
    contents = file_a.read()
    print(contents.rstrip())
    
    for line in file_a:
        print(line.rstrip())
        
    lines = file_a.readlines()

for linee in lines:
    print(linee.rstrip())

 10-2

with open(r'C:\Users\SAMSUNG\Desktop\python_work\zuoye_work\wt_canido.txt') as file_a :
    contents = file_a.read()
    print(contents.replace('python','C'))

 10-3

filename = 'guest.txt'
with open(filename,'w') as file_cc:
    message = input('请输入名字:')
    file_cc.write(message)

10-4

filename = 'guest2.txt'
with open(filename,'w') as file_dd:
    while True:
        message = input('请输入名字:')
        if message == 'quit':
            break;
        else:
            print('Hello , ' +message)
        file_dd.write(message + 'has logged in\n')
输出:
马自立has logged in
粽子has logged in

10-5略

10-6

try:
    int1 = input('请输入数字:')
    int2 = input('请输入数字:')
    answer = int(int1) + int(int2)
except ValueError:
    print('只能输入数字')
else:
    print(answer)  

10-7  摸鱼摸了太多天了 都忘了,我真sb

while True:
    try:
        int1 = input('请输入数字:')
        int2 = input('请输入数字:')
        answer = int(int1) + int(int2)
    except ValueError:
        print('只能输入数字')
    else:
        print(answer)
    if int1 == 'q':
        print('程序结束')
        break
输出:
请输入数字:333
请输入数字:789
1122
请输入数字:张
请输入数字:2
只能输入数字
请输入数字:q
请输入数字:2
只能输入数字
程序结束

10-8

def ent_files(filename):
    try:
        with open(filename) as file_ee:
            contents = file_ee.read()
    except FileNotFoundError:
        msg = "Sorry, the file "+ filename + "doesn't exist."
        print(msg)
    else:
        print(contents.rstrip())
        
filename = ['cats.txt','dogs.txt']
for filenamee in filename:
    ent_files(filenamee)
输出:
Sorry, the file cats.txtdoesn't exist.
dog1
dog2
dog3

10-9 except:

        pass

10-10 略。 网站不错~~

 10-11  真的很好笑,迷茫,失落,遭受打击的时候没人了解帮助你,终于踉跄走出来准备重新上路的时候,却开始时刻觊觎妄图透支你未来的光明。真是让人恶心啊。

import json

numb = input('你最喜欢的数字是?:')
filename = 'fav_numb.json'

with open(filename,'w') as file_gg:
    json.dump(numb,file_gg)
    
import json 
filename = 'fav_numb.json'
with open(filename) as file_ff:
    numb = json.load(file_ff)
    print('我知道你最喜欢的数字!是 ' + numb)

10-12 测试的时候修改文件名就行

import json
filename = 'fav_numb1.json'
try:
    with open(filename) as file_gg:
        numb = json.load(file_gg)
except FileNotFoundError:
    numb = input('请输入你最喜欢的数字是?:')
    with open(filename,'w') as file_ff:
        json.dump(numb,file_ff)
    print('我们会记住你最喜欢的数字.')
else:
    print('我知道你最喜欢的数字!是 ' + numb)

10-13 以后书上代码我都自己打,妈的抄了一份缩进全几把不对,我疯了 

import json
def get_sorted_username():
    """如果存储了用户名,就获取它"""
    filename = 'username.json'
    try:
        with open(filename) as f_obj:
            username = json.load(f_obj)
    except FileNotFoundError:
        return None
    else:
        return username

def get_new_username():
    """提示用户输入用户名"""
    username = input("What is your name?")
    filename = 'username.json'
    with open(filename, 'w') as f_obj:
        json.dump(username, f_obj)
    return username

def greet_user():
    """问候用户, 并指出其名字"""
    username = get_sorted_username()
    if username:
        result = input(username + "is right?Y or N:")
        if result == 'Y':
            print("Welcom back,"+username)
        else:
            username = get_new_username()
            print("We'll remember you when you come back,", username, "!")
greet_user()

 

posted on 2020-02-10 15:26  平凡的章鱼恶霸  阅读(187)  评论(0编辑  收藏  举报