File: merge_data.py

package info (click to toggle)
pytorch-cuda 2.6.0%2Bdfsg-7
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 161,620 kB
  • sloc: python: 1,278,832; cpp: 900,322; ansic: 82,710; asm: 7,754; java: 3,363; sh: 2,811; javascript: 2,443; makefile: 597; ruby: 195; xml: 84; objc: 68
file content (59 lines) | stat: -rw-r--r-- 1,885 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import sys


def merge_txt_files(file_list: list[str], output_file: str) -> None:
    if not file_list:
        print("No input files provided.")
        return

    metadata: list[str] = []
    content: list[str] = []

    # Read metadata and content from all files
    for file_path in file_list:
        try:
            with open(file_path) as file:
                lines = file.readlines()
                if len(lines) < 2:
                    print(
                        f"Error: {file_path} does not have enough lines for metadata."
                    )
                    return

                file_metadata = lines[:2]
                file_content = lines[2:]

                if not metadata:
                    metadata = file_metadata
                elif metadata != file_metadata:
                    print(f"Error: Metadata mismatch in {file_path}")
                    print("Expected metadata:")
                    print("".join(metadata))
                    print(f"Metadata in {file_path}:")
                    print("".join(file_metadata))
                    return

                content.extend(file_content)
        except OSError as e:
            print(f"Error reading file {file_path}: {e}")
            return

    # Write merged content to output file
    try:
        with open(output_file, "w") as outfile:
            outfile.writelines(metadata)
            outfile.writelines(content)
        print(f"Successfully merged files into {output_file}")
    except OSError as e:
        print(f"Error writing to output file {output_file}: {e}")


if __name__ == "__main__":
    if len(sys.argv) < 3:
        print(
            "Usage: python script.py output_file.txt input_file1.txt input_file2.txt ..."
        )
    else:
        output_file = sys.argv[1]
        input_files = sys.argv[2:]
        merge_txt_files(input_files, output_file)