Python file splitting and reorganization __python

Source: Internet
Author: User


File Split code:

#-*-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 ()

File Reorganization Code:

#-*-encoding:utf-8-*-

Import OS

def getfilesize (file):

File.seek (0, OS. Seek_end)

Filelength = File.tell ()

File.seek (0, 0)

Return filelength

Def rebuildfile ():

Filefullpath = r "%s"% raw_input ("File Base path:"). Strip ("\")

dividetotalpartscount = Int (raw_input ("How many parts have for you divided?:"))

File = open (Filefullpath, WB)

For I in Range (Dividetotalpartscount):

Filepartname = Filefullpath + ". Part" + STR (i+1)

Filepart = open (Filepartname, "RB")

Filepartsize = GetFileSize (Filepart)

Lengthwritten = 0

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.