Create utils.py
Browse files
utils.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import contextlib
|
3 |
+
|
4 |
+
@contextlib.contextmanager
|
5 |
+
def new_cd(x):
|
6 |
+
d = os.getcwd()
|
7 |
+
|
8 |
+
# This could raise an exception, but it's probably
|
9 |
+
# best to let it propagate and let the caller
|
10 |
+
# deal with it, since they requested x
|
11 |
+
os.chdir(x)
|
12 |
+
|
13 |
+
try:
|
14 |
+
yield
|
15 |
+
|
16 |
+
finally:
|
17 |
+
# This could also raise an exception, but you *really*
|
18 |
+
# aren't equipped to figure out what went wrong if the
|
19 |
+
# old working directory can't be restored.
|
20 |
+
os.chdir(d)
|