前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 文件合并 脚本

python 文件合并 脚本

作者头像
用户5760343
发布2022-05-13 11:10:28
9660
发布2022-05-13 11:10:28
举报
文章被收录于专栏:sktjsktj

!/usr/bin/python

""" ################################################################################ join all part files in a dir created by split.py, to re-create file. This is roughly like a 'cat fromdir/* > tofile' command on unix, but is more portable and configurable, and exports the join operation as a reusable function. Relies on sort order of filenames: must be same length. Could extend split/join to pop up Tkinter file selectors. ################################################################################ """

import os, sys readsize = 1024

def join(fromdir, tofile): output = open(tofile, 'wb') parts = os.listdir(fromdir) parts.sort() for filename in parts: filepath = os.path.join(fromdir, filename) fileobj = open(filepath, 'rb') while True: filebytes = fileobj.read(readsize) if not filebytes: break output.write(filebytes) fileobj.close() output.close()

if name == 'main': if len(sys.argv) == 2 and sys.argv[1] == '-help': print('Use: join.py [from-dir-name to-file-name]') else: if len(sys.argv) != 3: interactive = True fromdir = input('Directory containing part files? ') tofile = input('Name of file to be recreated? ') else: interactive = False fromdir, tofile = sys.argv[1:] absfrom, absto = map(os.path.abspath, [fromdir, tofile]) print('Joining', absfrom, 'to make', absto)

代码语言:javascript
复制
    try:
        join(fromdir, tofile)
    except:
        print('Error joining files:')
        print(sys.exc_info()[0], sys.exc_info()[1])
    else:
       print('Join complete: see', absto)
    if interactive: input('Press Enter key') # pause if clicked
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • !/usr/bin/python
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档