forked from Lemonzhulixin/python-appium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileOperate.py
More file actions
55 lines (51 loc) · 2.55 KB
/
FileOperate.py
File metadata and controls
55 lines (51 loc) · 2.55 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
import os
import shutil
class FileFilt:
fileList = [ ]
counter = 0
def __init__(self):
pass
def FindFile(self, find_str,file_format, path, filtrate=1):
for s in os.listdir(path):#返回指定目录下的所有文件和目录名
newDir = os.path.join(path, s) #将多个路径组合后返回,第一个绝对路径之前的参数将被忽略;os.path.join('路径','文件名.txt')
if os.path.isfile(newDir): #如果path是一个存在的文件,返回True。否则返回False。
if filtrate:
if newDir and (os.path.splitext(newDir)[1] in file_format) \
and (find_str in os.path.splitext(newDir)[0]): #os.path.splitext():分离文件名与扩展名
self.fileList.append(newDir)
self.counter += 1
else:
self.fileList.append(newDir)
self.counter += 1
def MoveFile(self, find_str, file_format, path, newpath, filtrate=1):
for s in os.listdir(path): # 返回指定目录下的所有文件和目录名
newDir = os.path.join(path, s) # 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略;os.path.join('路径','文件名.txt')
if os.path.isfile(newDir): # 如果path是一个存在的文件,返回True。否则返回False。
if filtrate:
if newDir and (os.path.splitext(newDir)[1] in file_format) \
and (find_str in os.path.splitext(newDir)[0]): # os.path.splitext():分离文件名与扩展名
self.fileList.append(newDir)
self.counter += 1
shutil.move(newDir, newpath)
else:
self.fileList.append(newDir)
self.counter += 1
def DelFolder(self, delDir):
delList = os.listdir(delDir)
for f in delList:
filePath = os.path.join(delDir, f)
if os.path.isfile(filePath):
os.remove(filePath)
print(filePath + " was removed!")
elif os.path.isdir(filePath):
shutil.rmtree(filePath, True)
print("Directory: " + filePath + " was removed!")
if __name__ == "__main__":
pass
find_str = 'XiaoYing-'
file_format = '.ips'
b = FileFilt()
b.FindFile(find_str,file_format, path="/Users/zhulixin/new")
for file in b.fileList:
filepath = os.path.abspath(file) #绝对路径
print(filepath)