• 合并文件(Python)


     1 # coding:utf-8
     2 
     3 import sys, os
     4 
     5 def showInfo():
     6     print """
     7     命令格式:mergefile -i srcfile1 srcfile2 ... -o dstfile
     8     说明:-i 选项用于指定多个文件或一个文件夹,程序会按照指定的顺序所有的文件及文件夹中的文件进行合并.
     9           -o 选项用于指定合并后的目标文件."""
    10     
    11     
    12 def mergeFiles(files, dstfilename):
    13     bufCount = 1024 * 1024
    14     dstfile = open(dstfilename, 'wb')
    15     
    16     for file in files:
    17         srcfile = open(file, 'rb')
    18         
    19         # 复制文件
    20         data = srcfile.read(bufCount)
    21         while '' != data:
    22             dstfile.write(data)
    23             data = srcfile.read(bufCount)
    24         
    25         srcfile.close()
    26         
    27     dstfile.close()
    28     
    29 
    30 if '__main__' == __name__:
    31     
    32     if sys.argv.count('-i') == 0 or sys.argv.count('-o') == 0:
    33         print "参数错误"
    34         showInfo()
    35         exit(1)
    36     
    37     cmds = ['-i''-o']
    38     filenames = []
    39     dstfile = ''
    40     
    41     i = 1
    42     while i < len(sys.argv):
    43         cmd = sys.argv[i]
    44         i += 1
    45         
    46         if 0 == cmds.count(cmd):
    47             print cmd
    48             print "参数错误啊"
    49             showInfo()
    50             exit(1)
    51         else:
    52             if cmd == '-i':
    53                 j = i
    54                 while j < len(sys.argv):
    55                     arg = sys.argv[j]
    56                     j += 1
    57                     
    58                     if 0 != cmds.count(arg):
    59                         i = j - 1
    60                         break
    61                     
    62                     if not os.path.exists(arg):
    63                         print '文件“', arg, '"不存在'
    64                         showInfo()
    65                         exit(2)
    66                     else:
    67                         if os.path.isfile(arg):
    68                             filenames.append(arg)
    69                         else:
    70                             dirfiles = os.listdir(arg)
    71                             for dfn in dirfiles:
    72                                 fn = os.path.join(arg, dfn)
    73                                 if os.path.isfile(fn):
    74                                     filenames.append(fn)
    75             elif cmd == '-o':
    76                 dstfile = sys.argv[i]
    77                 i += 1
    78                 if os.path.exists(cmd):
    79                     if os.path.isfile(cmd):
    80                         os.remove(cmd)
    81                     else:
    82                         print "参数错误:-o 后面应跟文件名"
    83                         showInfo()
    84                         exit(1)
    85     
    86     print "srcfiles:"
    87     for fn in filenames:
    88         print '  ', fn
    89     
    90     print 'dstfile:\n  ', dstfile
    91     
    92     mergeFiles(filenames, dstfile)
    93     print '合并成功'
  • 相关阅读:
    django form表单验证
    Django messages框架
    pymysql 操作数据库
    python数据类型详解及列表字典集合推导式详解
    深入flask中的request
    修改sqlarchemy源码使其支持jdbc连接mysql
    深入理解Python中协程的应用机制: 使用纯Python来实现一个操作系统吧!!
    svg坐标转换
    近几年总结
    frp中的json模块
  • 原文地址:https://www.cnblogs.com/hdtianfu/p/2223145.html
Copyright © 2020-2023  润新知