修改储存地址至"C:\Users\Lenovo\Desktop"
时间: 2023-07-24 17:16:07 浏览: 64
如果您想将合并后的文档存储在 "C:\Users\Lenovo\Desktop" 目录下,可以将代码中的 `your_directory` 替换为该目录路径。以下是修改后的示例代码:
```python
import os
def merge_txt_files(directory, output_file):
with open(output_file, 'w', encoding='utf-8') as outfile:
for filename in os.listdir(directory):
if filename.endswith(".txt"):
with open(os.path.join(directory, filename), 'r', encoding='utf-8') as infile:
outfile.write(infile.read())
outfile.write('\n') # 在每个文件内容之间添加换行符
# 设置目录和输出文件名
directory = 'C:\\Users\\Lenovo\\Desktop' # 替换为保存txt文件的目录
output_file = 'merged.txt' # 替换为合并后的输出文件名
# 调用函数进行合并
merge_txt_files(directory, output_file)
```
请确保目标目录 "C:\Users\Lenovo\Desktop" 存在,并且具有合适的写入权限。运行代码后,它将读取该目录中的所有txt文件,并将它们合并到一个名为 "merged.txt" 的文档中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)