用python新建一个文本文件hello_world.txt,写入五行“hello,world!:1-5”并打印
时间: 2023-10-23 11:08:25 浏览: 376
hello-world-python
可以使用以下代码来实现:
```python
with open('hello_world.txt', 'w') as file:
for i in range(1, 6):
file.write(f'hello, world!: {i}\n')
with open('hello_world.txt', 'r') as file:
print(file.read())
```
输出结果为:
```
hello, world!: 1
hello, world!: 2
hello, world!: 3
hello, world!: 4
hello, world!: 5
```
阅读全文