-
Notifications
You must be signed in to change notification settings - Fork 0
/
chdir_context.py
43 lines (35 loc) · 995 Bytes
/
chdir_context.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
'''
A context to temporarily cd to another directory.
Frequently useful: ChdirAlongside(__file__)
'''
import os
class ChdirContext:
def __init__(self, to):
self.stack = []
self(to)
def __call__(self, to):
self.to = to
return self
def __enter__(self):
self.stack.append(os.getcwd())
os.chdir(self.to)
return self
def __exit__(self, type, value, traceback):
os.chdir(self.stack.pop(-1))
def ChdirAlongside(filename):
return ChdirContext(os.path.dirname(filename))
if __name__ == '__main__':
print('demo')
print('Going from', os.getcwd())
with ChdirAlongside(__file__):
print('to', os.getcwd())
print('and back to', os.getcwd())
print('Stacking')
print(os.getcwd())
with ChdirContext('d:/') as cdc:
print(os.getcwd())
with cdc('d:/temp'):
print(os.getcwd())
print(os.getcwd())
print(os.getcwd())
input('enter...')