-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpath.py
130 lines (113 loc) · 4.41 KB
/
dpath.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
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
#!/usr/bin/python
# -*- coding: <<encoding>> -*-
# ------------------------------------------------------------------------------
# File with functions to deal with paths and files
# 1. Get list of directories inside a directory
# 2. Get list of files inside a directory
# 3. Extract zip file
# ------------------------------------------------------------------------------
import os
from os import path
from os import rename
from os import listdir
import zipfile
import random
import string
from pprint import pprint
class PathNotExists(Exception):
pass
class UnableToMakeDir(Exception):
pass
# ------------------------------------------------------------------------------
# Extract zip <file> in <folder>
# ------------------------------------------------------------------------------
def extract_zip(file, folder):
if os.path.exists(file):
zip_ref = zipfile.ZipFile(e, 'r')
if not os.path.exists(folder):
try:
os.mkdir(folder)
except:
raise UnableToMakeDir
zip_ref.extractall(folder)
zip_ref.close()
# ------------------------------------------------------------------------------
# Get the lists of the directories
# Returns < [Absolute Directory Paths], [Directory Names (without path)] >
# Raises PathNotExists if folder does not exist
# ------------------------------------------------------------------------------
def get_folders_in_folder(folder):
if os.path.exists(folder):
# directories contenute in folder
dirs = [f for f in os.listdir(folder) if path.isdir(os.path.join(folder,f))]
# costruzione del path assoluto
abs_dirs = [path.join(folder,f) for f in dirs]
return abs_dirs, dirs
else:
raise PathNotExists
# ------------------------------------------------------------------------------
# Get the lists of the file into the directory
# <estens> filter to a certain extension, could be a list of extensions
# extension must be specified with "dot" .ext
# Return < [Absolute File Paths], [File names (without path)]
# Raises PathNotExists if folder does not exist
# ------------------------------------------------------------------------------
def get_files_in_folder(folder, extens = "all files"):
if os.path.exists(folder):
# files contenute in folder
all_files = [f for f in os.listdir(folder) if path.isfile(os.path.join(folder,f))]
if extens == "all files":
files = all_files
else:
if not isinstance(extens,list):
ext = [extens]
else:
ext = extens
files = [f for f in all_files if path.splitext(f)[1] in ext]
# costruzione del path assoluto
abs_files = [path.join(folder,f) for f in files]
return abs_files, files
else:
raise PathNotExists
# Cancella tutti i files che hanno nel nome match
def delete_files_with_matches_name(folder, match, extens = 'all files'):
abpaths , _ = get_files_in_folder(folder, extens)
todelete = [f for f in abpaths if match in f]
print("deleting ", len(todelete), " files")
for f in todelete:
try:
os.remove(f)
except:
print("Unable to delete ", f)
if __name__ == "__main__":
a,b = get_folders_in_folder('.')
print("Absolute path")
print(a)
print("Directory names")
print(b)
print("------------------------------------------------")
a,b = get_files_in_folder('.')
print("Get file Absolute path")
print(a)
print("File names")
print(b)
print("------------------------------------------------")
a,b = get_files_in_folder('.',".py")
print("Get file Absolute path")
print(a)
print("File names")
print(b)
print("------------------------------------------------")
a,b = get_files_in_folder('.', [".py",".ini"])
print("Get file Absolute path")
print(a)
print("File names")
print(b)
try:
a,b = get_folders_in_folder('.\\topolino')
except:
print("Directory does not exist")
a,b = get_folders_in_folder('.\\images')
print(a)
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
delete_files_with_matches_name(path.join('.','images'), 'retro', extens = '.jpg')