Python第十章(文件与异常)课后习题

10-1 py学习笔记:

with open('learning_python.txt') as file_object:
	content=file_object.read()
	print(content,end='')
print()
with open('learning_python.txt') as file_object:
	for line in file_object:
		print(line,end='')
print()
with open('learning_python.txt') as file_object:
	lines=file_object.readlines()
for line in lines:
	print(line,end='')

运行结果:

10-2 C语言学习笔记:

with open('learning_python.txt') as file_object:
	lines=file_object.readlines()
for line in lines:#replace 只是返回值不修改
	print(line.replace('Python','C'),end='')

运行结果:

10-3 访客:编写一个程序,提示用户输入其名字;用户作出响应后,将其名字写
入到文件 guest.txt 中。
 

# 	file_object.write('\nhhh')
name=input('Please input your name:\n')
with open('ftest.txt','w') as file_object:
	file_object.write(name)

运行结果:

 

10-4 访客名单:编写一个 while 循环,提示用户输入其名字。用户输入其名字后,
在屏幕上打印一句问候语,并将一条访问记录添加到文件 guest_book.txt 中。确保这个
文件中的每条记录都独占一行。
 


                
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值