编程语言
首页 > 编程语言> > 14python open with 读写文件

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

标签:__,utf,encoding,读写,readfile,14python,line,open
来源: https://blog.csdn.net/qq_35871505/article/details/118058625