Many files are provided and need to be analyzed. if reading multiple files at a time causes a lot of trouble, merge and pre-process the source files. Two usage modes are supported:
(1) merge all files in a folder (ignore non-file entries such as folders)
(2) merge multiple files displayed.
The code is as follows:
Import sys
Import OS
'''
Usage (1): merge_files pathname
Pathname is directory and merge files in pathname directory
Usage (2): merge_files file1 file2 [file3 [...]
'''
FILE_SLIM = (256 * (1024*1024) #256 M match 2 ** n
Def merge_files (fileslist, mfname ):
Global FILE_SLIM
P_fp = open (mfname, "wba ")
For file in fileslist:
With open (file, "rb") as c_fp:
Fsize = OS. stat (file). st_size
Count = fsize & FILE_SLIM
While count> 0:
P_fp.write (c_fp.read (FILE_SLIM ))
Fsize-= FILE_SLIM
Count-= 1
P_fp.write (c_fp.read ())
P_fp.close
Def main ():
Argc = len (sys. argv)-1
Fileslist = []
If argc = 2:
Dir_name = OS. path. realpath (sys. argv [1])
Assert (OS. path. isdir (dir_name ))
File_dir = OS. listdir (dir_name)
Fileslist = [OS. path. join (dir_name, file) for file in file_dir if OS. path. isfile (OS. path. join (dir_name, file)]
Print (fileslist)
Elif argc> = 3:
Fileslist = [OS. path. realpath (sys. argv [index]) for index in range (1, argc) if OS. path. isfile (OS. path. realpath (sys. argv [index])]
Merge_files (fileslist, sys. argv [argc])
If _ name _ = '_ main __':
Main ()