-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdirectory_manager.py
28 lines (22 loc) · 968 Bytes
/
directory_manager.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
#!/usr/bin/python
import os
import urlparse
#Create a new directory if the current directory doesn't exist
def check_create_directory(web_page_directory, local_file_path, root_directory):
current_directory = local_file_path
#Create the directories if they don't exist
url_components = urlparse.urlparse(web_page_directory)
folder_path = url_components.path.split('/')
#Remove empty elements from folder path
del folder_path[-1]
folder_path = filter(None, folder_path)
#Concatenate all folders in relative local file path
folder_path_string = ''
for folder in folder_path[::-1]:
folder_path_string = folder + '/' + folder_path_string
#Create the directory if it doesn't exist
if folder_path_string is not '':
current_directory = os.path.join(root_directory, folder_path_string)
if not os.path.exists(current_directory):
os.makedirs(current_directory)
return current_directory