合并多个txt文件带换行符
时间: 2023-08-04 07:08:30 浏览: 48
您可以使用以下 Python 代码将多个 TXT 文件合并为一个文件,并在文件中保留原始换行符:
```python
import glob
# 获取所有 TXT 文件的文件名
files = glob.glob('*.txt')
# 打开输出文件
with open('output.txt', 'w') as outfile:
# 遍历所有文件
for file in files:
# 打开每个文件并将其内容写入输出文件
with open(file, 'r') as infile:
outfile.write(infile.read())
```
这段代码将所有 TXT 文件合并为一个名为 `output.txt` 的新文件,并在文件中保留原始换行符。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![xltm](https://img-home.csdnimg.cn/images/20210720083646.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)
![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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)