-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathftools.py
More file actions
61 lines (50 loc) · 1.52 KB
/
ftools.py
File metadata and controls
61 lines (50 loc) · 1.52 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
from os.path import *
from os import listdir, makedirs, remove
import pickle
import shutil
def fileparts(path): # path = file path
[p,f] = split(path)
[n,e] = splitext(f)
return [p,n,e]
def listfiles(path,token): # path = folder path
l = []
for f in listdir(path):
fullPath = join(path,f)
if isfile(fullPath) and token in f:
l.append(fullPath)
l.sort()
return l
def listsubdirs(path): # path = folder path
l = []
for f in listdir(path):
fullPath = join(path,f)
if isdir(fullPath):
l.append(fullPath)
l.sort()
return l
def pathjoin(p,ne): # '/path/to/folder', 'name.extension' (or a subfolder)
return join(p,ne)
def saveData(data,path,verbose=False):
if verbose:
print('saving data')
dataFile = open(path, 'wb')
pickle.dump(data, dataFile)
def loadData(path,verbose=False):
if verbose:
print('loading data')
dataFile = open(path, 'rb')
return pickle.load(dataFile)
def createFolderIfNonExistent(path):
if not exists(path): # from os.path
makedirs(path)
def removeFolderIfExistent(path):
if exists(path):
shutil.rmtree(path)
def moveFile(fullPathSource,folderPathDestination):
[p,n,e] = fileparts(fullPathSource)
shutil.move(fullPathSource,pathjoin(folderPathDestination,n+e))
def copyFile(fullPathSource,folderPathDestination):
[p,n,e] = fileparts(fullPathSource)
shutil.copy(fullPathSource,pathjoin(folderPathDestination,n+e))
def removeFile(path):
remove(path)