python 1、创建文件,名称为“english_ song”, 将歌词按行输入2、将文件重命名为“my heart wil|goon”3、对文件创建副本为“back-up"4、关闭文件
时间: 2024-01-22 08:20:18 浏览: 138
以下是 Python 代码实现:
```python
# 创建文件并输入歌词
with open('english_song.txt', 'w') as f:
f.write('Every night in my dreams\n')
f.write('I see you, I feel you\n')
f.write('That is how I know you go on\n')
f.write('Far across the distance\n')
f.write('And spaces between us\n')
f.write('You have come to show you go on\n')
f.write('Near, far, wherever you are\n')
f.write("I believe that the heart does go on\n")
f.write("Once more, you open the door\n")
f.write("And you're here in my heart\n")
f.write('And my heart will go on and on\n')
# 将文件重命名为"my heart will go on"
import os
os.rename('english_song.txt', 'my heart will go on.txt')
# 创建文件副本为"back-up"
import shutil
shutil.copy('my heart will go on.txt', 'back-up.txt')
# 关闭文件
f.close()
```
阅读全文