python 合并文件的具体实例
程序员文章站
2024-01-20 13:48:34
...
支持两种用法:
(1)合并某一文件夹下的所有文件(忽略文件夹等非文件条目)
(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()
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()
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
相关文章
相关视频
专题推荐
-
独孤九贱-php全栈开发教程
全栈 170W+
主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门
-
玉女心经-web前端开发教程
入门 80W+
主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门
-
天龙八部-实战开发教程
实战 120W+
主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习
推荐阅读
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论