遍历目录下的文件每250M打包一个文件
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
#!/usr/bin/env python
# -*- utf-8 -*-
# @Time ? ? : 2018/7/20 0020 下午 10:16
# @Author ? : 陳元
# @Email ? ?: abcmeabc@163.com
# @file ? ? : tarFile.py
import os
import tarfile
import threading
import time
class TarFile():
? ? def __init__(self):
? ? ? ? pass
? ? #打包多個文件
? ? def tarFileList(self,fname,flist):
? ? ? ? tar = tarfile.open(fname+'.tar','w')
? ? ? ? for file in flist:
? ? ? ? ? ? tar.add(file)
? ? ? ? tar.close()
? ? def tar(self,fname):
? ? ? ? tar = tarfile.open(fname+'.tar',"w")
? ? ? ? for root,dir,files in os.walk(fname):
? ? ? ? ? ? for file in files:
? ? ? ? ? ? ? ? fpath = os.path.join(root,file)
? ? ? ? ? ? ? ? tar.add(fpath)
? ? ? ? tar.close()
? ? def untar(self,fname,dirs):
? ? ? ? tar = tarfile.open(fname)
? ? ? ? tar.extractall(path=dirs)
? ? def getFileList(self,path):
? ? ? ? start = time.time()
? ? ? ? dirs = os.listdir(path)
? ? ? ? for dir in dirs:
? ? ? ? ? ? newPath = os.path.join(path,dir)
? ? ? ? ? ? if os.path.isdir(newPath):
? ? ? ? ? ? ? ? print(dir)
? ? ? ? ? ? ? ? self.getChildFileList(newPath,dir)
? ? ? ? end = time.time()
? ? ? ? print("all time : {0}".format(end-start))
? ? ? ? pass
? ? # 遞歸子目錄
? ? def getChildFileList(self,path,fname):
? ? ? ? dirs = os.listdir(path)
? ? ? ? tFileList = []
? ? ? ? fileNum = 0
? ? ? ? sizeSum = 0
? ? ? ? count = len(dirs)
? ? ? ? thread = []
? ? ? ? for dir in dirs:
? ? ? ? ? ? count = count - 1
? ? ? ? ? ? newPath = os.path.join(path,dir)
? ? ? ? ? ? fname = fname+'_'+dir
? ? ? ? ? ? if os.path.isdir(newPath):
? ? ? ? ? ? ? ? #self.getChildFileList(newPath,fname)
? ? ? ? ? ? ? ? t = threading.Thread(target=self.getChildFileList,args=(newPath,dir))
? ? ? ? ? ? ? ? t.start()
? ? ? ? ? ? ? ? thread.append(t)
? ? ? ? ? ? elif os.path.isfile(newPath):
? ? ? ? ? ? ? ? size = os.path.getsize(newPath)
? ? ? ? ? ? ? ? sizeSum = sizeSum + size
? ? ? ? ? ? ? ? sizeM = sizeSum/1024/1024
? ? ? ? ? ? ? ? if sizeM < 250:
? ? ? ? ? ? ? ? ? ? tFileList.append(dir)
? ? ? ? ? ? ? ? elif sizeM >= 250 or count == 0:
? ? ? ? ? ? ? ? ? ? os.chdir(path)
? ? ? ? ? ? ? ? ? ? print(path)
? ? ? ? ? ? ? ? ? ? fileNum = fileNum + 1
? ? ? ? ? ? ? ? ? ? fname = fname + '_' + str(fileNum)
? ? ? ? ? ? ? ? ? ? #self.tarFileList(fname,tFileList)
? ? ? ? ? ? ? ? ? ? # t = threading.Thread(target=self.tarFileList,args=(fname, tFileList))
? ? ? ? ? ? ? ? ? ? # t.start()
? ? ? ? ? ? ? ? ? ? # thread.append(t)
? ? ? ? ? ? ? ? ? ? print(fname)
? ? ? ? ? ? ? ? ? ? sizeSum = 0
? ? ? ? ? ? ? ? ? ? del tFileList[:]
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? print("error... size")
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print("error...not dir file")
? ? ? ? for t in thread:
? ? ? ? ? ? t.join()
? ? ? ? pass
def run():
? ? pass
def main():
? ? # try:
? ? ? ? tFile = TarFile()
? ? ? ? tFile.getFileList("V:\\")
? ? # except :
? ? # ? ? print("error")
? ? # pass
if __name__ == '__main__':
? ? main()
?
轉(zhuǎn)載于:https://my.oschina.net/u/3824134/blog/1860417
總結(jié)
以上是生活随笔為你收集整理的遍历目录下的文件每250M打包一个文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构p91
- 下一篇: Aidl进程间通信详细介绍