读写文档,Python引入了with语句,自动调用close(),方法:with open(’/path/to/file’, ‘r’) as f,
并使用utf-8编码,encoding = ‘utf-8’,
# __author__ = 'lzc'
# -*- coding: UTF-8 -*-
def readfile(srcpath):
with open(srcpath,'r',encoding = 'utf-8',) as f:
for line in f.readlines():
print(line.strip('\n'))
if __name__=="__main__":
readfile("example.txt")