提供了很多个文件,需要对文件分析,如果每次读取多个文件,造成很多麻烦,所以需要对源文件进行合并预处理。
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)) #256M 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()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。