-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtils.py
More file actions
130 lines (121 loc) · 4.12 KB
/
FileUtils.py
File metadata and controls
130 lines (121 loc) · 4.12 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
# -*- coding:utf-8 -*-
import os
import shutil
'''
创建文件夹
'''
def mkdir(path):
try:
folder = os.path.exists(path)
if not folder:
os.makedirs(path)
else:
print("There is this folder")
return True
except Exception as e:
print('mkdir error: ',e)
return False
'''
移动文件:将srcPath/fileName 移动到 dstPath/fileName
moveFile('dataset/pic','dataset/txt','09.jpg')
'''
def moveFile(srcPath,dstPath,fileName):
print('from: ',srcPath,' to: ', dstPath)
try:
f_src = os.path.join(srcPath,fileName)
mkdir(dstPath)
f_dst = os.path.join(dstPath,fileName)
shutil.move(f_src,f_dst)
return True
except Exception as e:
print('move file error: ',e)
return False
'''
拷贝文件:将srcPath/fileName 拷贝到 dstPath/fileName
moveFile('dataset/pic','dataset/txt','09.jpg')
'''
def copyFile(srcPath,dstPath,fileName):
print('from: ',srcPath,' to: ', dstPath)
try:
f_src = os.path.join(srcPath,fileName)
mkdir(dstPath)
f_dst = os.path.join(dstPath,fileName)
shutil.copyfile(f_src,f_dst)
return True
except Exception as e:
print('copy file error: ',e)
return False
'''
修改文件后缀名
修改dirPath路径下,fileType类型的后缀名为 newFileType
changeSuffix('dataset/pic','dataset/pic/',"jpg","png")
'''
def changeSuffix(dirPath,newPath,fileType,newFileType):
try:
g = os.walk(dirPath)
for maindir,subdir,filename_list in g:
for filename in filename_list:
if filename.endswith(fileType):
newFileName = os.path.splitext(filename)[0] + "." + newFileType
newFileDir = newPath + newFileName
os.rename(os.path.join(maindir,filename),newFileDir)
return True
except Exception as e:
print('change suffix error: ',e)
return False
'''
查找文件,返回文件(加路径)
dirPath路径下,fileType格式的文件
searchFile('dataset/pic','jpg','png')
'''
def searchFile(dirPath,*fileType):
try:
g = os.walk(dirPath)
file_path_name = [os.path.join(maindir,filename) for maindir,subdir,filename_list in g for filename in filename_list
if filename.endswith(fileType)]
return file_path_name
except Exception as e:
print('search file error: ',e)
return False
'''
查找文件,返回文件名(不带路径)
'''
def searchFileReturnName(dirPath,*fileType):
try:
g = os.walk(dirPath)
file_name = [filename for maindir,subdir,filename_list in g for filename in filename_list
if filename.endswith(fileType)]
return file_name
except Exception as e:
print('search file error: ',e)
return False
'''
查找两个文件夹中不同名的文件
serachDiffFile('dataset/pic','dataset/txt','txt','jpg','png')
'''
def serachDiffFile(dirPathA,dirPathB,fileTypeA,*fileTypeB):
try:
fileNameA = searchFileReturnName(dirPathA,*fileTypeB)
fileNameB = searchFileReturnName(dirPathB,fileTypeA)
file_list_A = [os.path.splitext(x)[0] for x in fileNameA]
file_list_B = [os.path.splitext(y)[0] for y in fileNameB]
diff = list(set(file_list_A).difference(set(file_list_B))) #a 有 b没有
diff2 = list(set(file_list_B).difference(set(file_list_A))) #b 有 a没有
return diff,diff2
except Exception as e:
print('search file error: ',e)
return False
if __name__ == '__main__':
#print(serachDiffFile('dataset/pic', 'dataset/xml'))
#print(moveFile('dataset/pic','dataset/txt','09.jpg'))
#changeSuffix('dataset/pic','dataset/pic/',"jpg","png")
#print(searchFileReturnName('dataset/pic','jpg','png'))
#print(serachDiffFile('dataset/pic','dataset/txt','txt','jpg','png'))
#copyFile('dataset/pic', 'dataset/txt', '09.png')
#拷贝文件夹中制定类型的文件
result = searchFileReturnName('dataset/pic','png')
if not result:
print('error')
else:
for i in result:
copyFile('dataset/pic','dataset/txt',i)