-
Notifications
You must be signed in to change notification settings - Fork 2
/
depthSearch.py
51 lines (41 loc) · 1.34 KB
/
depthSearch.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
#!/usr/bin/python
#coding=utf-8
'''
Basic recursion program that shows all elements in depth in the directory tree
of the specified absolute path in the searchPath variable
Outputs into two files:
depthSearch_rawOutput.txt: shows files in the order of recursion
depthSearch_stackOutput.txt: shows files after ordering in a stack
@author: J. Alvarez
'''
import os
searchPath = "/home/jcalvarezj/Documents/hyperblog"
rawFileManager = open('depthSearch_rawOutput.txt','w')
stackFileManager = open('depthSearch_stackOutput.txt','w')
outputStack = []
def depthSearch(directory, level):
branches = os.listdir(directory)
for item in branches:
itemPath = os.path.join(directory, item)
directorySlash = ""
if (os.path.isdir(itemPath)):
depthSearch(itemPath, level*2)
directorySlash = "/"
hearts = ""
for i in range(level):
hearts += "♥"
rawFileManager.write(hearts+" "+item+directorySlash+"\n")
outputStack.append(hearts+" "+item+directorySlash+"\n")
def wrapper(path):
rawFileManager.write("Searching in depth for the following path: %s\n" % \
path)
depthSearch(path,1)
writeStackFile(path)
def writeStackFile(path):
stackFileManager.write("Searching in depth for the following path: %s\n" % \
path)
while(outputStack):
stackFileManager.write(outputStack.pop())
wrapper(searchPath)
rawFileManager.close()
stackFileManager.close()