Path Management

Goal

  • Normalize paths on different platform
  • Create, copy and remove folders
  • Handle errors

Modules


In [ ]:
import os
import os.path
import shutil
import errno
import glob
import sys

See also:

  • pathlib on Python 3.4+

In [ ]:
# Be python3 ready
from __future__ import unicode_literals, print_function

Multiplatform Path Management

1- The os.path module seems verbose but it's the best way to manage paths. It's:

  • safe
  • multiplatform

2- Here we check the operating system and prepend the right path


In [ ]:
import os
import sys
basedir, hosts  = "/", "etc/hosts"

In [ ]:
# sys.platform shows the current operating system
if sys.platform.startswith('win'):
    basedir = 'c:/windows/system32/drivers'
print(basedir)

In [ ]:
# Join removes redundant "/"
hosts = os.path.join(basedir, hosts)
print(hosts)

In [ ]:
# normpath fixes "/" orientation 
# and redundant ".."
hosts = os.path.normpath(hosts)
print("Normalized path is", hosts)

In [ ]:
# realpath resolves symlinks (on unix)
! mkdir -p /tmp/course
! ln -sf /etc/hosts /tmp/course/hosts
realfile = os.path.realpath("/tmp/course/hosts") 
print(realfile)

In [ ]:
# Exercise: given the following path
base, path = "/usr", "/bin/foo"
# Which is the expected output of result?
result = os.path.join(base, path)

Manage trees

Python modules can:

- manage directory trees
- and basic errors

In [ ]:
# os and shutil supports basic file operations
# like recursive copy and tree creation.
from os import makedirs
makedirs("/tmp/course/foo/bar")

In [ ]:
# while os.path can be used to test file existence
from os.path import isdir
assert isdir("/tmp/course/foo/bar")

In [ ]:
# Check the directory content with either one of
!tree /tmp/course || find /tmp/course

In [ ]:
# We can use exception handlers to check
#  what happened.

try:
    # python2 does not allow to ignore
    #  already existing directories
    #  and raises an OSError
    makedirs("/tmp/course/foo/bar")
except OSError as e:
    # Just use the errno module to
    #  check the error value
    print(e)
    import errno
    assert e.errno == errno.EEXIST

In [ ]:
from shutil import copytree, rmtree
# Now copy recursively two directories
# and check the result
copytree("/tmp/course/foo", "/tmp/course/foo2")
assert isdir("/tmp/course/foo2/bar")

In [ ]:
#This command should work on both unix and windows 
!dir /tmp/course/foo2/

In [ ]:
# Now remove it and check the outcome
rmtree("/tmp/course/foo")
assert not isdir("/tmp/course/foo/bar")

In [ ]:
#This command should work on both unix and windows 
!dir /tmp/course/

In [ ]:
# Cleanup created files
rmtree("/tmp/course")

In [ ]: