难度:
点击显示代码
1 2 3 4 5 6 7 8 9 10 11 12
fname = '123.txt' # 写入文件内容 data = '\n'.join([f'第{i}行内容' for i in range(10)]) with open(fname, 'w') as f: f.write(data) # 读取文件内容 with open(fname) as f: data2 = f.read() for line in data2.split('\n'): print(line)
1 2 3 4 5 6 7 8 9 10
第0行内容 第1行内容 第2行内容 第3行内容 第4行内容 第5行内容 第6行内容 第7行内容 第8行内容 第9行内容
可以看到当前目录生成了一个新的文件 123.txt,打开看看它的内容
123.txt
=== 全文完 ===