创建文件,名称为“english_song”,将歌词按行输入,将文件重名名为“my heart will go on”对文件创建副本“back-up”关闭文件
时间: 2023-10-04 17:04:45 浏览: 68
这是使用 Python 语言实现的:
```
# 创建文件 english_song 并按行输入歌词
with open("english_song", "w") as file:
file.write("Every night in my dreams\n")
file.write("I see you, I feel you\n")
file.write("That is how I know you go on\n")
file.write("Far across the distance\n")
file.write("And spaces between us\n")
file.write("You have come to show you go on\n")
file.write("Near, far, wherever you are\n")
file.write("I believe that the heart does go on\n")
file.write("Once more, you open the door\n")
file.write("And you're here in my heart\n")
file.write("And my heart will go on and on\n")
# 将文件重命名为 my heart will go on
import os
os.rename("english_song", "my heart will go on")
# 创建文件副本 back-up
import shutil
shutil.copyfile("my heart will go on", "back-up")
# 关闭文件
file.close()
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)