笨方法学习Python-习题15: 读取文件

来源:互联网 发布:淘宝联盟 微信遭投诉 编辑:程序博客网 时间:2024/06/06 01:32
"""我们已经学习了input和argv,只有多做练习才能明白其工作原理。这节练习设计到了两个文件,一个是ex15.py,另一个是ex15_sample.txt,内容如下:This is stuff I typed into a file.It is really cool stuff.Lots and lots of fun to have in here."""# coding=utf-8# 导入模组from sys import argv# 定义变量script,filename = argv# 打开文件,可以在Windows命令窗口输入 python -m pydoc opentxt = open(filename)# 打印print("Here's your file %r:" % filename)# 读取文件内容并打印print(txt.read())txt.close()# 打印“Type the filename again:”print("Type the filename again:")# 输入文件名file_again = input("> ")# 打开文件txt_again = open(file_again)# 读取文件内容并打印print(txt_again.read())txt_again.close()"""1.txt and txt_again 变量执行一下 close() ,处理完文件后你需要将其关闭,这是很重要的一点。2.from sys import argv 是什么意思? sys 是一个代码库,是从库里取出 argv 这个功能来3.总结下节练习的知识点:open(),close(),read()方法,以及argv和input()方法的加深使用""" 

原创粉丝点击