python编写程序,复制当前目录下的文件file1.txt,生成file2.txt。
时间: 2023-06-22 15:28:23 浏览: 178
可以使用Python的shutil库中的copy()函数来实现文件复制。
以下是代码示例:
```python
import shutil
# 复制文件
shutil.copy("file1.txt", "file2.txt")
```
这会将当前目录下的file1.txt复制为file2.txt。如果目标文件已存在,则会覆盖它。
相关问题
编写程序,复制当前目录下的文件file1.txt,生成新文件file2.txt。用python
可以使用Python的shutil模块中的copy函数来实现复制文件。具体步骤如下:
```python
import shutil
# 源文件路径
src_file = "./file1.txt"
# 目标文件路径
dst_file = "./file2.txt"
# 复制文件
shutil.copy(src_file, dst_file)
```
这个程序会将当前目录下的file1.txt文件复制为file2.txt文件。如果file2.txt文件已经存在,则会覆盖原有的文件。
编写程序,打开一篇英文文章(存在当前目录下的文件in.txt中),为该文章生成词汇表(存到当前目录下的另一个文件words.txt中)python
以下是一个简单的 Python 程序,可用于读取一个英文文本文件,并生成包含所有单词的词汇表:
```python
import string
with open("in.txt", "r") as file:
text = file.read()
# Remove punctuation and convert to lowercase
text = text.translate(str.maketrans("", "", string.punctuation)).lower()
# Split into words
words = text.split()
# Remove duplicates and sort alphabetically
words = sorted(set(words))
# Save words to file
with open("words.txt", "w") as file:
for word in words:
file.write(word + "\n")
```
这个程序首先打开名为 "in.txt" 的文件,并读取其中的文本。然后,它将所有标点符号删除,并将所有字母转换为小写。接下来,它将文本拆分为单词,并从中删除重复项,并按字母顺序排序。最后,它将单词写入名为 "words.txt" 的文件中。