python 读写txt文件

读文件

#!/usr/bin/env python
#coding: utf-8
file=open('aa.txt'):
for line in file:
    line=line.strip()
    print line

写文件

#!/usr/bin/env python
#coding: utf-8
with open('test_w.txt', mode='a', encoding='utf8') as f:
    lines = [
            'this is my text for write,',
            'hello world!',
            'bye'
        ]
    for line in lines:
        if line:line=line+'\n'
        f.write(line)
    f.close()
posted @ 2022-02-17 23:34  lifei888  阅读(17)  评论(0编辑  收藏  举报