python 合并文件的具体实例

(编辑:jimmy 日期: 2025/2/27 浏览:2)

支持两种用法:
(1)合并某一文件夹下的所有文件(忽略文件夹等非文件条目)
(2)显示的合并多文件。
复制代码 代码如下:
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()
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。