读写文件
读文件
file=open('aa.txt')
for line in file:
line=line.strip()
print(line)
写文件
with open('test_w.txt', mode='w', encoding='utf8') as f:
lines = [
'this is my text for write,',
'hello world!',
'bye'
]
# 注意 writelines 不会写入换行,需要我们自己加。
f.writelines(lines)
# 以上等同于以下
lines = [
'this is my text for write,\n',
'hello world!\n',
'bye\n'
]
# 注意 writelines 不会写入换行,需要我们自己加。
for line in lines:
line=line+'\n'
f.write(line)