#-*-encoding:utf-8-*- Import OS Import Sys Import threading def getfilesize (file): File.seek (0, OS. Seek_end) Filelength = File.tell () File.seek (0, 0) Return filelength Def dividefile (): Filefullpath = r "%s"% raw_input ("File path:"). Strip ("\") dividetotalpartscount = Int (raw_input ("How many parts does you like to divide?:")) If Os.path.exists (Filefullpath): File = open (Filefullpath, ' RB ') FileSize = GetFileSize (file) File.close () # Send file Content For I in Range (Dividetotalpartscount): Filepartsender = Threading. Thread (Target=seperatefilepart, args= (Filefullpath, Dividetotalpartscount, i+1, fileSize)) Filepartsender.start () For I in Range (Dividetotalpartscount): Sem.acquire () Os.remove (Filefullpath) Else Print "File doesn ' t exist" def seperatefilepart (Filefullpath, Dividetotalpartscount, Threadindex, fileSize): Try # Calculate start position and end position Filepartsize = Filesize/dividetotalpartscount StartPosition = Filepartsize * (threadIndex-1) #print "Thread:%d, startposition:%d"% (Threadindex, startposition) endposition = filepartsize * threadIndex-1 if Threadindex = = Dividetotalpartscount: Endposition = fileSize-1 Filepartsize = Filesize-startposition File = open (Filefullpath, "RB") File.seek (startposition) Filepartname = Filefullpath + ". Part" + STR (THREADINDEX) Filepart = open (Filepartname, "WB") Lengthwritten = 0 While Lengthwritten < filepartsize: Buflen = 1024 Lengthleft = Filepartsize-lengthwritten If Lengthleft < 1024: Buflen = Lengthleft BUF = File.read (Buflen) Filepart.write (BUF) Lengthwritten = Len (buf) Filepart.close () File.close () Sem.release () Print "Part%d finished, size%d"% (Threadindex, filepartsize) Except Exception, E: Print E SEM = threading. Semaphore (0) While True: Dividefile () |