Upload layer_diff_dataset/cat_txt.py with huggingface_hub
Browse files
layer_diff_dataset/cat_txt.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
# import
|
| 3 |
+
# root_dir = '/mnt/workspace/workgroup/sihui.jsh/layer_diff_dataset/train'
|
| 4 |
+
# txt_list = ['im_rgba1.txt','im_rgba2.txt','im_rgba3.txt','im_rgba4.txt','im_rgba5.txt','im_rgba6.txt']
|
| 5 |
+
|
| 6 |
+
def merge_txt_files(directory, output_file):
|
| 7 |
+
"""
|
| 8 |
+
将指定目录下的所有文本文件合并为一个文件。
|
| 9 |
+
|
| 10 |
+
参数:
|
| 11 |
+
- directory: 存放文本文件的目录路径。
|
| 12 |
+
- output_file: 合并后生成文件的路径。
|
| 13 |
+
"""
|
| 14 |
+
# 获取目录下所有的.txt文件
|
| 15 |
+
txt_files = [f for f in os.listdir(directory) if f.endswith('.txt')]
|
| 16 |
+
|
| 17 |
+
# 按文件名排序,以确保文件按顺序合并
|
| 18 |
+
txt_files.sort()
|
| 19 |
+
|
| 20 |
+
# 开始合并文件
|
| 21 |
+
with open(output_file, 'w', encoding='utf-8') as outfile:
|
| 22 |
+
for filename in txt_files:
|
| 23 |
+
filepath = os.path.join(directory, filename)
|
| 24 |
+
|
| 25 |
+
# 将每个文件的内容追加到输出文件
|
| 26 |
+
with open(filepath, 'r', encoding='utf-8') as infile:
|
| 27 |
+
outfile.write(infile.read())
|
| 28 |
+
# 确保每个文件内容之间有明确的分隔
|
| 29 |
+
# outfile.write('\n')
|
| 30 |
+
|
| 31 |
+
print(f"All text files have been merged into {output_file}.")
|
| 32 |
+
|
| 33 |
+
# 替换为你的源目录
|
| 34 |
+
source_directory = '/mnt/workspace/workgroup/sihui.jsh/layer_diff_dataset/train'
|
| 35 |
+
# 替换为你希望输出的文件路径和名称
|
| 36 |
+
output_filepath = os.path.join(source_directory,'im_rgba.txt')
|
| 37 |
+
|
| 38 |
+
merge_txt_files(source_directory, output_filepath)
|