服务器之家

服务器之家 > 正文

python文件拆分与重组实例

时间:2021-04-27 00:23     来源/作者:远行的风

文件拆分代码:

python" id="highlighter_310168">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#-*-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 do 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()

文件重组代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#-*-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 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
 
  while lengthwritten < filepartsize:
 
   buflen = 1024
 
   buf = filepart.read(buflen)
 
   file.write(buf)
 
   lengthwritten += len(buf)
 
  filepart.close()
 
  os.remove(filepartname)
 
 file.close()
 
 
 
while true:
 
 rebuildfile()

拆分文件演示:

源文件:

python文件拆分与重组实例

拆分:

python文件拆分与重组实例

拆分后文件:

python文件拆分与重组实例

重组文件:

python文件拆分与重组实例

重组后文件:

python文件拆分与重组实例

以上这篇python文件拆分与重组实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qwertyupoiuytr/article/details/66616934

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
返回顶部