脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python文件拆分与重组实例

python文件拆分与重组实例

2021-04-27 00:23远行的风 Python

今天小编就为大家分享一篇python文件拆分与重组实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

文件拆分代码:

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

延伸 · 阅读

精彩推荐