-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtil.py
More file actions
285 lines (236 loc) · 8.31 KB
/
Copy pathFileUtil.py
File metadata and controls
285 lines (236 loc) · 8.31 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/python
# coding=utf-8
import sys
import zipfile
import shutil
import os
import os.path
import time
import datetime
import json
import requests
from xml.dom.minidom import parse
sys.path.insert(0,os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from Common.Platform import Platform
class FileUtil():
@staticmethod
def CopyDir(src,dst,isRemove=True):
flag = os.path.exists(dst)
if flag:
if isRemove:
shutil.rmtree(dst)
shutil.copytree(src,dst)
@staticmethod
def CopyFile(src, dst):
if os.path.isfile(src):
shutil.copyfile(src,dst)
# 复制单个文件
@staticmethod
def CopyOneFile(sourceFile, targetFile):
if os.path.isfile(sourceFile):
# shutil.copyfile(src,dst)
open(targetFile, "wb").write(open(sourceFile, "rb").read())
#把某一目录下的所有文件复制到指定目录中
@staticmethod
def CopyFiles(sourceDir, targetDir):
if sourceDir.find(".svn") > 0:
return
for file in os.listdir(sourceDir):
sourceFile = os.path.join(sourceDir, file)
targetFile = os.path.join(targetDir, file)
if os.path.isfile(sourceFile):
if not os.path.exists(targetDir):
os.makedirs(targetDir)
if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))):
open(targetFile, "wb").write(open(sourceFile, "rb").read())
# if os.path.isdir(sourceFile):
# First_Directory = False
# copyFiles(sourceFile, targetFile)
#复制一级目录下的所有文件到指定目录:
@staticmethod
def CoverFiles(sourceDir, targetDir):
for file in os.listdir(sourceDir):
# 过滤文件
if file == "Thumbs.db":
continue
sourceFile = os.path.join(sourceDir, file)
# sourceFile= os.path.normpath(sourceFile)
targetFile = os.path.join(targetDir, file)
# targetFile= os.path.normpath(targetFile)
#cover the files
if os.path.isfile(sourceFile):
# open(targetFile, "wb").write(open(sourceFile, "rb").read())
# flag = os.path.exists(targetFile)
# if flag:
# os.remove(targetFile)
dir = FileUtil.GetLastDirofDir(targetFile)
if os.path.exists(dir)==False:
os.mkdir(dir)
shutil.copyfile(sourceFile, targetFile)
#目录嵌套
if os.path.isdir(sourceFile):
print(sourceFile)
print(targetFile)
FileUtil.CoverFiles(sourceFile,targetFile)
# 获取子目录列表
@staticmethod
def GetSubDirList(sourceDir,listdir):
for file in os.listdir(sourceDir):
# 过滤文件
if file == "Thumbs.db":
continue
sourceFile = os.path.join(sourceDir, file)
#目录嵌套
if os.path.isdir(sourceFile):
print(sourceFile)
listdir.append(sourceFile)
# 获取子目录文件列表
@staticmethod
def GetSubFileList(sourceDir,ext,listfile):
for file in os.listdir(sourceDir):
sourceFile = os.path.join(sourceDir, file)
#cover the files
if os.path.isfile(sourceFile):
# print sourceFile
# 分割文件名与后缀
temp_list = os.path.splitext(file)
# name without extension
name = temp_list[0]
# 后缀名,包含. 例如: ".apk "
ext_file = temp_list[1]
ext_file = ext_file[1:]
if ext==ext_file:
print(sourceFile)
listfile.append(sourceFile)
#目录嵌套
if os.path.isdir(sourceFile):
# print sourceFile
FileUtil.GetSubFileList(sourceFile,ext,listfile)
#删除一级目录下的所有文件:
@staticmethod
def RemoveFileInFirstDir(targetDir):
for file in os.listdir(targetDir):
targetFile = os.path.join(targetDir, file)
if os.path.isfile(targetFile):
os.remove(targetFile)
# 复制单个文件
@staticmethod
def CopyOneFile(sourceFile, targetFile):
if os.path.isfile(sourceFile):
open(targetFile, "wb").write(open(sourceFile, "rb").read())
@staticmethod
def SaveString2File(str, file):
f = open(file, 'wb') # 若是'wb'就表示写二进制文件
b = str.encode('utf-8',"ignore")
f.write(b)
f.close()
@staticmethod
def SaveByte2File(b, file):
f = open(file, 'wb') # 若是'wb'就表示写二进制文件
f.write(b)
f.close()
@staticmethod
def GetFileString(filePath):
f = open(filePath, 'rb')
strFile = f.read().decode('utf-8',"ignore")
f.close()
return strFile
#last dir
@staticmethod
def GetLastDir():
return GetLastDirofDir(cur_file_dir())
@staticmethod
def GetLastDirofDir(path):
str_split = '/'
if Platform.isWindowsSystem():
str_split = '\\'
idx = path.rfind(str_split)
ret = path[0:idx]
return ret
def GetDirOfPath(path):
str_split = '/'
if Platform.isWindowsSystem():
str_split = '\\'
idx = path.rfind(str_split)
ret = path[0:idx]
return ret
# 不包含.
@staticmethod
def GetFileExt(path):
idx = path.rfind(".")
if idx<0:
return ""
idx=idx+1
ret = path[idx:]
# print(path="+path+" idx="+str(idx)+" ret="+ret+" slen="+str(slen)
return ret
@staticmethod
def GetFileName(path):
idx = path.rfind(".")
if idx<0:
return path
ret = path[0:idx]
return ret
@staticmethod
def GetPathNameWithExt(path):
idx = path.rfind("/")
if idx<0:
idx = path.rfind("\\")
if idx<0:
return path
ret = path[(idx+1):]
return ret
@staticmethod
def GetPathNameWithoutExt(path):
return FileUtil.GetFileName(FileUtil.GetPathNameWithExt(path))
@staticmethod
def GetPathName():
return GetDirNameofPath(getLastDir())
@staticmethod
def GetDirNameofPath(path):
str_split = '/'
if Platform.isWindowsSystem():
str_split = '\\'
idx = path.rfind(str_split)
s_len=len(path)
game = path[idx+1:s_len]
return game
@staticmethod
def CreateDir(dir):
if os.path.exists(dir)==False:
os.mkdir(dir)
# 复制单个文件
@staticmethod
def CreateFile(sourceFile):
open(sourceFile, "w").write("")
@staticmethod
# 循环创建
def CreateDir2(dir):
dirlast = FileUtil.GetDirOfPath(dir)
listdir = []
while(True):
if os.path.exists(dirlast)==False:
# print("dirlast=",dirlast)
listdir.insert(0,dirlast)
dirlast = FileUtil.GetDirOfPath(dirlast)
else:
break
ext = FileUtil.GetFileExt(dir)
# print("CreateDir2 ext=",ext)
if len(ext)==0:
listdir.append(dir)
for dirtmp in listdir:
if os.path.exists(dirtmp)==False:
os.mkdir(dirtmp)
@staticmethod
def RemoveDir(dir):
if os.path.exists(dir)==True:
shutil.rmtree(dir)
@staticmethod
def RemoveFile(filePath):
if os.path.exists(filePath)==True:
os.remove(filePath)
@staticmethod
def Geturl(url):
r = requests.get(url)
return r.content.decode('utf-8',"ignore")