向文本文件(InFile.txt)中写入一首唐诗与一段英文表达,并以自己的学号和 姓名结尾。显示文件的内容,并将 InFile 存放到 d:/目录下,最后将 InFile.txt 文 件的内容复制到另外一个目录下的 OutFile.txt 中。
时间: 2024-05-14 11:18:14 浏览: 75
以下是Python代码实现:
```python
# 写入文件
with open("d:/InFile.txt", "w", encoding="utf-8") as f:
f.write("悯农\n"
"锄禾日当午,\n"
"汗滴禾下土。\n"
"谁知盘中餐,\n"
"粒粒皆辛苦。\n"
"\n"
"The Love Song of J. Alfred Prufrock\n"
"Let us go then, you and I,\n"
"When the evening is spread out against the sky\n"
"Like a patient etherized upon a table;\n"
"Let us go, through certain half-deserted streets,\n"
"The muttering retreats\n"
"Of restless nights in one-night cheap hotels\n"
"And sawdust restaurants with oyster-shells:\n"
"Streets that follow like a tedious argument\n"
"Of insidious intent\n"
"To lead you to an overwhelming question...\n"
"\n"
"学号:123456,姓名:张三")
# 读取文件并输出内容
with open("d:/InFile.txt", "r", encoding="utf-8") as f:
print(f.read())
# 复制文件
import shutil
shutil.copy("d:/InFile.txt", "d:/OutFile.txt")
```
注意,这里使用了 Python 标准库中的 `shutil` 模块来实现文件复制功能。
阅读全文