1
+ import argparse
2
+ import os
3
+ import re
4
+
5
+
6
+ def time_to_seconds (time_str ):
7
+ h , m , s = map (int , time_str .split (':' ))
8
+ return h * 3600 + m * 60 + s
9
+
10
+ def merge_txt_files (folder_path ):
11
+ output_file = os .path .join (folder_path , 'output.txt' )
12
+
13
+ with open (output_file , 'w' ) as outfile :
14
+ # Sort files to ensure consistent ordering
15
+ files = sorted ([f for f in os .listdir (folder_path ) if f .endswith (".txt" ) and f != 'output.txt' ])
16
+
17
+ for index , filename in enumerate (files , start = 1 ):
18
+ file_path = os .path .join (folder_path , filename )
19
+ with open (file_path , 'r' ) as infile :
20
+ for line in infile :
21
+ # Extract timestamp
22
+ match = re .match (r'\[(\d{2}:\d{2}:\d{2})\](.*)' , line )
23
+ if match :
24
+ time_str , content = match .groups ()
25
+ seconds = time_to_seconds (time_str )
26
+ # Write the modified line
27
+ outfile .write (f"{ index } -[{ seconds } ]{ content .strip ()} \n " )
28
+
29
+ print (f"All text files merged into { output_file } " )
30
+
31
+ if __name__ == "__main__" :
32
+ parser = argparse .ArgumentParser (description = "Merge all text files in the folder into output.txt" )
33
+ parser .add_argument ('folder_path' , type = str , help = 'Path to the folder containing the text files' )
34
+
35
+ args = parser .parse_args ()
36
+ merge_txt_files (args .folder_path )
0 commit comments