forked from facebookresearch/segment-anything
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathosTools.py
72 lines (59 loc) · 1.87 KB
/
osTools.py
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
import os
import os.path as osp
def mkdir(path) :
if osp.exists(path) :
return
try :
os.mkdir(path)
except FileNotFoundError :
parentPath, _ = osp.split(path)
mkdir(parentPath)
os.mkdir(path)
def getBaseName(fullName) :
return osp.splitext(osp.split(fullName)[1])[0]
def zipDirs (dirList) :
"""
A common operation is to get SVGs
or graphs from different directories
and match them up and perform
some operations on them.
Parameters
----------
dirList : list
List of directories to zip.
"""
filesInDirList = list(map(listdir, dirList))
return zip(*filesInDirList)
def listdir (path) :
"""
Convenience function to get
full path details while calling os.listdir
Also ensures that the order is always the same.
Parameters
----------
path : str
Path to be listed.
"""
paths = [osp.join(path, f) for f in os.listdir(path)]
paths.sort()
return paths
def allfiles (directory) :
""" List full paths of all files/directory in directory """
for f in listdir(directory) :
yield f
if osp.isdir(f) :
yield from allfiles(f)
def allFilesWithSuffix(directory, suffix) :
""" List full paths of all files that end with suffix """
return filter(lambda x : x.endswith(suffix), allfiles(directory))
def allDirs (directory) :
""" List all directories within this directory """
return filter(osp.isdir, listdir(directory))
def allFiles (directory) :
""" List all files that are not directories in this directory """
return filter(lambda x : not osp.isdir(x), listdir(directory))
def relpathToAbsPath (x) :
"""
Returns the absolute path for a relative path. Can be convenient in some scenarios.
"""
return osp.normpath(osp.join(osp.split(osp.abspath(__file__))[0], x))