forked from ywangd/stash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcd.py
43 lines (33 loc) · 1.17 KB
/
cd.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
#!/usr/bin/env python
########################################################################.......
"""Change the current working directory.
"""
from __future__ import print_function
import argparse
import os
import sys
def main(args):
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("dir", action="store", nargs="?",
default=os.environ["HOME2"],
help="the new working directory")
ns = p.parse_args(args)
status = 0
try:
if os.path.exists(ns.dir):
if os.path.isdir(ns.dir):
# chdir does not raise exception until listdir is called, so check for access here
if os.access(ns.dir, os.R_OK):
os.chdir(ns.dir)
else:
print('cd: {} access denied'.format(ns.dir))
else:
print('cd: %s: Not a directory' % ns.dir)
else:
print ('cd: %s: No such file or directory' % ns.dir)
except Exception as err:
print("cd: {}: {!s}".format(type(err).__name__, err), file=sys.stderr)
status = 1
sys.exit(status)
if __name__ == "__main__":
main(sys.argv[1:])