2019独角兽企业重金招聘Python工程师标准>>>
#!/usr/bin/env python
# -*- utf-8 -*-
# @Time : 2018/7/20 0020 下午 10:16
# @Author : 陈元
# @Email : [email protected]
# @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()