用Python实现以下功能 1、创建文件,名称为english_song”,将 歌词按行输入, 歌词是“every night in ay dreane,i see you,i feel you. that is how I lcnow you go an.far across the distance. and spaces betueen us,you have cane to showr you go on. near far,wherever you are. I belleve,that the heart does go on. once nore you open the door,and youre here in ny heart. and ay beart will go on and on,lovel can touch us one tine.”2、将文件重命名为“my heart will go on”3、对文件创建副本为“back- up”4、关闭文件
时间: 2023-12-10 18:39:12 浏览: 106
用Python 完成常见的任务
5星 · 资源好评率100%
下面是Python代码实现以上功能:
```python
# 创建文件并写入歌词
with open("english_song.txt", "w") as f:
f.write("every night in ay dreane,i see you,i feel you. that is how I lcnow you go an.far across the distance. and spaces betueen us,you have cane to showr you go on. near far,wherever you are. I belleve,that the heart does go on. once nore you open the door,and youre here in ny heart. and ay beart will go on and on,lovel can touch us one tine.")
# 将文件重命名
import os
os.rename("english_song.txt", "my heart will go on.txt")
# 创建文件副本
import shutil
shutil.copyfile("my heart will go on.txt", "back-up.txt")
# 关闭文件
f.close()
```
注意:以上代码需要把歌词中的句子拆成多行写入,因为在 Python 中,字符串如果过长,可能会导致代码格式不整洁,影响可读性。
阅读全文