python写1、创建文件,名称为"english. song”,将歌词按行输入2、将文件重命名为"my heart willgoon" 3、对文件创建副本为"back-up"4、关闭文件 文件内容为every night in my dreams, i see you, i feel youo that is how i know you go on, far across the distanceoand spaces between us, you have come to show you go ononear far, wherever you are. i believe, that the heart does go ono once more you open the door, and youre here in my heartoand myу heart will go on and on, lovel can touch us one time o
时间: 2023-12-10 10:38:32 浏览: 29
下面是Python代码实现:
```python
# 创建文件并写入歌词
with open('english.song', '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 youre here in my heart\n')
f.write('and my heart will go on and on,\n')
f.write('love can touch us one time\n')
# 重命名文件
import os
os.rename('english.song', 'my heart will go on')
# 创建文件副本
import shutil
shutil.copyfile('my heart will go on', 'back-up')
# 关闭文件
f.close()
```
以上代码中,使用`with open()`语句创建并打开文件,并使用`write()`方法将歌词写入文件。然后使用`os.rename()`方法将文件重命名为"my heart will go on"。接着使用`shutil.copyfile()`方法创建文件副本"back-up"。最后使用`close()`方法关闭文件。