Python Merge file examples-Python tutorial

Source: Internet
Author: User
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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.