"""
我们已经学习了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 open
txt = 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()方法的加深使用
"""
笨方法学习Python-习题15: 读取文件
最新推荐文章于 2021-03-26 18:24:40 发布