14python open with 读写文件

读写文档,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")
上一篇:【JS学习】require('fs')(fs模块用于对系统文件及目录进行读写操作。)


下一篇:Cypress系列(94)- readFile() 命令详解