This repository has been archived by the owner on Mar 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
directorytools.py
98 lines (78 loc) · 3.15 KB
/
directorytools.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
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
import re
from os import walk, getcwd, path, mkdir, unlink
from shutil import rmtree
default_dir = getcwd()
def file_by_name(file_name, directory=default_dir):
file_list = files_by_name(file_name, directory)
if file_list:
return file_list[0]
def files_by_name(file_name, directory=default_dir):
return find_files(re.compile(file_name), directory)
def file_by_extension(file_extension, directory=default_dir):
file_list = files_by_extension(file_extension, directory)
if file_list:
return file_list[0]
def files_by_extension(file_extension, directory=default_dir):
if not file_extension.startswith("."):
file_extension = "." + file_extension
return find_files(re.compile(".*\\" + file_extension), directory)
def file_by_partial(file_partial, directory=default_dir):
file_list = files_by_partial(file_partial, directory)
if file_list:
return file_list[0]
def files_by_partial(file_partial, directory=default_dir):
return find_files(re.compile(".*" + file_partial + ".*"), directory)
def find_files(regex, directory=default_dir):
file_list = []
for root, dirs, dirfiles in walk(directory):
if root.find("/.") > 0: #hidden folders
continue
for name in dirfiles:
if regex.match(name) and not name.startswith("."):
file_list.append(root + name)
if len(file_list) > 0:
return file_list
def file_list(directory=default_dir):
return find_files(re.compile(".*"), directory)
def folder_by_name(folder_name, directory=default_dir):
folder_list = folders_by_name(folder_name, directory)
if folder_list:
return folder_list[0]
def folders_by_name(folder_name, directory=default_dir):
return find_folders(re.compile(folder_name), directory)
def folder_by_partial(folder_partial, directory=default_dir):
folder_list = folders_by_partial(folder_partial, directory)
if folder_list:
return folder_list[0]
def folders_by_partial(folder_partial, directory=default_dir):
return find_folders(re.compile(".*" + folder_partial + ".*"), directory)
def find_folders(regex, directory=default_dir):
folder_list = []
for root, dirs, dirfiles in walk(directory):
if root.find("/.") > 0: #hidden folders
continue
for dir_name in dirs:
if regex.match(dir_name) and not dir_name.startswith("."):
folder_list.append(root + "/" + dir_name)
if len(folder_list) > 0:
return folder_list
def folder_list(directory=default_dir):
return find_folders(re.compile(".*"), directory)
def create_directory(directory):
if not path.exists(directory):
mkdir(directory)
def clear_directory(directory):
if not path.exists(directory):
return
for root, dirs, files in walk(directory):
for f in files:
unlink(path.join(root, f))
for d in dirs:
rmtree(path.join(root, d))
def clear_or_create(directory):
create_directory(directory)
clear_directory(directory)
#add other directory name cleaning operations as they come up
def clean_dir_name(directory):
if not directory.endswith("/"):
return directory + "/"