python - 7.读取文件内容

版权声明:经验之谈,不知能否换包辣条,另,转载请注明出处。 https://blog.csdn.net/zhezhebie/article/details/89436274

读取文件内容:

from sys import argv

script ,filename = argv

print(f"你要打开的是{filename}:")
# text = open(filename)
text = open(filename,'r', encoding='UTF-8')
content = text.read(10)
print(f"文件内容是:{content}")
text.close()

another_file = input("另一个文件名字: ")
another_text = open(another_file,'r',encoding = "utf-8")
print(another_text.read())
another_text.close()

报错了,gbk编码不能解码.
在这里插入图片描述

open的时候需要设置编码集:
在这里插入图片描述

在cmd里面用python读取文件内容:
在这里插入图片描述

python书籍:
https://github.com/yidao620c/python3-cookbook/blob/master/source/index.rst
https://python3-cookbook.readthedocs.io/zh_CN/latest/
https://www.xncoding.com/2017/01/22/fullstack/readthedoc.html

猜你喜欢

转载自blog.csdn.net/zhezhebie/article/details/89436274