python 合并文件的具体实例
时间:2022-03-12 03:18:26
支持两种用法:
(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()
标签:python,合并,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
网页特效文字之—银箔字
2013-08-07 00:21:39
![](https://img.aspxhome.com/file/UploadPic/20072/20072311613309s.jpg)
Webform 内置对象 Session对象、Application全局对象,ViewState详细介绍
2023-06-30 02:14:07
Mootools 1.2教程(1)——MooTools介绍
2008-11-12 13:01:00
![](https://img.aspxhome.com/file/UploadPic/up/9-Mootools-1.2.gif)
Python基于百度AI实现抓取表情包
2022-05-01 00:01:35
![](https://img.aspxhome.com/file/2023/3/83913_0s.jpg)
如何在django中实现分页功能
2021-10-21 03:29:49
![](https://img.aspxhome.com/file/2023/9/86319_0s.png)
python 实现Flask中返回图片流给前端展示
2023-12-01 06:58:23
Python虚拟机字节码教程之装饰器实现详解
2021-10-31 08:59:47
![](https://img.aspxhome.com/file/2023/1/100761_0s.png)
浅谈在django中使用filter()(即对QuerySet操作)时踩的坑
2022-05-03 10:24:49
![](https://img.aspxhome.com/file/2023/6/101736_0s.jpg)
对python .txt文件读取及数据处理方法总结
2022-08-04 02:36:07
![](https://img.aspxhome.com/file/2023/5/65975_0s.jpg)
Python如何用filter函数筛选数据
2022-07-23 21:20:14
详解 Python中LEGB和闭包及装饰器
2023-05-19 00:16:41
pytorch中fuse_modules源码解读
2023-09-15 20:58:01
![](https://img.aspxhome.com/file/2023/7/97347_0s.png)
DW实现滚动新闻
2007-12-03 11:35:00
解析SQL Server与ASP互操作的时间处理
2009-02-01 16:40:00
python爬取酷狗音乐排行榜
2021-01-24 19:43:49
Python如何自动获取目标网站最新通知
2021-07-14 18:18:16
![](https://img.aspxhome.com/file/2023/0/95900_0s.jpg)
WEB页面工具语言XML应用分类之运用
2008-05-29 10:58:00
导入pytorch时libmkl_intel_lp64.so找不到问题解决
2021-03-21 01:52:23
如何在MySQL查询结果集中得到记录行号
2008-12-17 15:00:00
asp读取xml实例代码
2011-03-08 11:13:00