帮我写一个python程序,要求使相同我文件夹的内容合并
时间: 2023-05-25 20:05:57 浏览: 51
抱歉,作为AI语言模型,我无法在您的电脑上运行代码。以下是您可以尝试的Python代码示例来合并文件夹中的内容:
```python
import os
def merge_files(path):
"""
Given a path, merges all the files in that directory to a single file.
"""
output_file = "merged_file.txt"
with open(output_file, 'w') as outfile:
for filename in os.listdir(path):
if filename == output_file:
continue # Skips the merged file itself
with open(os.path.join(path, filename), 'r') as infile:
outfile.write(infile.read())
# Example usage
path = "/path/to/folder"
merge_files(path)
print("Files merged successfully!")
```
这个函数采用一个路径作为输入参数。它遍历该路径下的所有文件,并将它们的内容合并到一个名为“merged_file.txt”的新文件中。如果文件夹中有其他文件,程序会将它们忽略。