首页 > 其他 > 详细

splitFile2SmallFile

时间:2015-09-21 23:59:46      阅读:497      评论:0      收藏:0      [点我收藏+]

 

 

技术分享
 1 """
 2 this is aa customizable version of the standard unix split command-line
 3 utility;because it is written in python,it also works on windows and can be 
 4 easily modifyed;because it export a function,its logic can also be imported
 5 and resued in other applications
 6 """
 7 import sys,os
 8 kilobytes =1024
 9 megabytes = kilobytes*1000
10 chunksize = int(1.4* megabytes)                  #default roughtly a floppy
11 
12 def split(fromfile,todir,chunksize=chunksize):
13     if not os.path.exists(todir):                 # caller handlers errors
14         os.mkdir(todir)                          #make dir,read/write parts
15     else:
16         for fname in os.listdir(todir):
17             os.remove(os.path.join(todir,fname))  #delete any exiting files
18     partnum =0
19     input = open(fromfile,rb)
20     while True:
21         chunk = input.read(chunksize)
22         if not chunk:break
23         partnum +=1
24         filename = os.path.join(todir,(part%04d % partnum))
25         fileobj = open(filename,wb)
26         fileobj.write(chunk)
27         fileobj.close()
28     input.close()
29     assert partnum<=9999
30     return partnum
31 
32 if __name__ ==__main__:
33     if len(sys.argv) == 2 and sys.argv[1]== -help:
34         print(use:split.py [file to split target-dir [chunksize]])
35     else:
36         if len(sys.argv) <3:
37             interactive =True
38             fromfile =input(File to be split?)
39             todir = input(directory to store part files?)
40         else:
41             interactive = False
42             fromfile,todir = sys.argv[1:3]
43             if len(sys.argv) == 4:chunksize =int(sys.argv[3])
44         absfrom,absto = map(os.path.abspath,[fromfile,todir])
45         print(splitting,absfrom,to,absto,by,chunksize)
46         try:
47             parts = split(fromfile,todir,chunksize)
48         except:
49             print(error during split:)
50             print(sys.exc_info()[0],sys.exc_info()[1])
51         else:
52             print(split finished:,parts,parts are in ,absto)
53         if interactive:
54             input(press enter key)  #pause if clicked
55             
View Code

技术分享

技术分享

技术分享

split to 200k

技术分享

技术分享

splitFile2SmallFile

原文:http://www.cnblogs.com/lxk613/p/4827677.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!