python - 7. read the contents of the file

Copyright: the voice of experience, I do not know whether the change package spicy bar, and the other, please indicate the source. https://blog.csdn.net/zhezhebie/article/details/89436274

Read the contents of the file:

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()

It is given, gbk coding can not be decoded.
Here Insert Picture Description

open when the need to set the encoding sets:
Here Insert Picture Description

Read the contents of the file with the python cmd in which:
Here Insert Picture Description

python books:
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

Guess you like

Origin blog.csdn.net/zhezhebie/article/details/89436274