使用Python创建名为”Blowing in the wind.txt”的文件。 (2)在文件中添加如下内容: How many roads must a man walk down Before they call him a man How many seas must a white dove sail Before she sleeps in the sand (3)在文件头部插入歌名:Blow in the wind。 (4)在歌名后插入歌手名:Bob Dylan (5)在文件末尾加上字符串:1962 by Warner Bros.Inc. (6)在屏幕上打印文件内容。
时间: 2023-12-03 08:46:18 浏览: 145
使用Python播放音乐
以下是实现以上要求的Python代码:
```python
# 创建文件并添加内容
with open("Blowing in the wind.txt", "w") as f:
f.write("How many roads must a man walk down\nBefore they call him a man\nHow many seas must a white dove sail\nBefore she sleeps in the sand")
# 在文件头部插入歌名
with open("Blowing in the wind.txt", "r+") as f:
content = f.read()
f.seek(0, 0)
f.write("Blowin' in the wind\n" + content)
# 在歌名后插入歌手名
with open("Blowing in the wind.txt", "r+") as f:
content = f.read()
f.seek(0, 0)
f.write("Blowin' in the wind\nby Bob Dylan\n" + content[len("Blowin' in the wind\n"):])
# 在文件末尾加上字符串
with open("Blowing in the wind.txt", "a") as f:
f.write("\n1962 by Warner Bros.Inc.")
# 打印文件内容
with open("Blowing in the wind.txt", "r") as f:
print(f.read())
```
输出:
```
Blowin' in the wind
by Bob Dylan
How many roads must a man walk down
Before they call him a man
How many seas must a white dove sail
Before she sleeps in the sand
1962 by Warner Bros.Inc.
```
阅读全文