The OS Module


In [ ]:
import os

Inspecting the Execution Environment


In [ ]:
print "curdir:", os.curdir
print "getcwd:", os.getcwd()
os.chdir("..")
print "new cwd:", os.getcwd()

In [ ]:
print os.path.abspath(os.curdir)
print os.path.abspath("../../..")

In [ ]:
for f in ("getpid", "getuid", "geteuid", "getgid", "getegid", "uname"):
    print"{:>12}:".format(f), getattr(os, f)()

In [ ]:
for k, v in os.environ.items():
    print "{:>30}: {}".format(k, v)

Working With Files and Directories


In [ ]:
scratch = os.path.expanduser("~/tmp/scratch")
print scratch

In [ ]:
os.mkdir(scratch)
print "{:o}".format(os.stat(scratch).st_mode)[1:]
os.chmod(scratch, 0700)
print "{:o}".format(os.stat(scratch).st_mode)[1:]

In [ ]:
for name in ("one", "two", "three"):
    subdir = os.path.join(scratch, name)
    os.mkdir(subdir)
print os.listdir(scratch)

In [ ]:
os.rmdir(scratch)

In [ ]:
import shutil
shutil.rmtree(scratch)

In [ ]:
os.listdir(scratch)