简单demo

with open('d:/pydemo/pythonStart/fun1.py', 'r') as f:
    #print(f.read())
    while True:
        line = f.readline()
        #print(line)
        if line == None or line == "" :
            break;
        # strip是去除行尾的\n,不然print会输出一行 \n又空一行
        print(line.strip())

文件读写模式

'r' open for reading (default)
'w' open for writing, truncating the file first
'x' open for exclusive creation, failing if the file already exists
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)

参考文章

python 文件读写
python官方文档

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。