python 合并文件的具體實例
更新時間:2013年08月08日 11:39:27 作者:
提供了很多個文件,需要對文件分析,如果每次讀取多個文件,造成很多麻煩,所以需要對源文件進(jìn)行合并預(yù)處理。
支持兩種用法:
(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()
(1)合并某一文件夾下的所有文件(忽略文件夾等非文件條目)
(2)顯示的合并多文件。
復(fù)制代碼 代碼如下:
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()
相關(guān)文章
Python實現(xiàn)的遞歸神經(jīng)網(wǎng)絡(luò)簡單示例
這篇文章主要介紹了Python實現(xiàn)的遞歸神經(jīng)網(wǎng)絡(luò),是一篇摘錄自github代碼片段的文章,涉及Python遞歸與數(shù)學(xué)運算相關(guān)操作技巧,需要的朋友可以參考下2017-08-08Python THREADING模塊中的JOIN()方法深入理解
這篇文章主要介紹了Python THREADING模塊中的JOIN()方法深入理解,本文用簡潔易懂的語言總結(jié)了對JOIN()方法的理解,不同于其它文章,需要的朋友可以參考下2015-02-02Python實現(xiàn)簡易過濾刪除數(shù)字的方法小結(jié)
這篇文章主要介紹了Python實現(xiàn)簡易過濾刪除數(shù)字的方法,結(jié)合實例形式總結(jié)分析了Python基于正則及內(nèi)置函數(shù)過濾刪除數(shù)字的相關(guān)操作技巧,需要的朋友可以參考下2019-01-01Python通用循環(huán)的構(gòu)造方法實例分析
這篇文章主要介紹了Python通用循環(huán)的構(gòu)造方法,結(jié)合實例形式分析了Python常見的交互循環(huán)、哨兵循環(huán)、文件循環(huán)、死循環(huán)等實現(xiàn)與處理技巧,需要的朋友可以參考下2018-12-12Python使用正則表達(dá)式報錯:nothing?to?repeat?at?position?0的解決方案
今天在使用python 正則模塊匹配字符串時遇到了這個問題,分享給大家,這篇文章主要給大家介紹了關(guān)于Python使用正則表達(dá)式報錯nothing?to?repeat?at?position?0的解決方案,需要的朋友可以參考下2023-03-03Python讀取配置文件(config.ini)以及寫入配置文件
這篇文章主要介紹了Python讀取配置文件(config.ini)以及寫入配置文件,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python實現(xiàn)平行坐標(biāo)圖的繪制(plotly)方式
今天小編就為大家分享一篇Python實現(xiàn)平行坐標(biāo)圖的繪制(plotly)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11Python Pillow Image.save 保存為jpg圖片壓縮問題
Pillow 庫支持多種圖片格式,Pillow 能夠很輕松地實現(xiàn)圖片格式之間的轉(zhuǎn)換。本文就來詳細(xì)的介紹一下Image.save的具體使用,感興趣的可以了解一下2021-11-11