-
Notifications
You must be signed in to change notification settings - Fork 13
/
walk_up.py
50 lines (39 loc) · 1.01 KB
/
walk_up.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
import os
from os import path
def walk_up(bottom):
"""
mimic os.walk, but walk 'up'
instead of down the directory tree
"""
bottom = path.realpath(bottom)
# get files in current dir
try:
names = os.listdir(bottom)
except Exception as e:
print(e)
return
dirs, nondirs = [], []
for name in names:
if path.isdir(path.join(bottom, name)):
dirs.append(name)
else:
nondirs.append(name)
yield bottom, dirs, nondirs
new_path = path.realpath(path.join(bottom, '..'))
# see if we are at the top
if new_path == bottom:
return
for x in walk_up(new_path):
yield x
if __name__ == '__main__':
# tests/demos
# print all files and directories
# directly above the current one
for i in walk_up(os.curdir):
print(i)
# look for a TAGS file above the
# current directory
for c, d, f in walk_up(os.curdir):
if 'TAGS' in f:
print(c)
break