Manipulating pathnames

Let's explore a little more the abilities of os.path by means of a simple example:

# files/paths.py
import os

filename = 'fear.txt'
path = os.path.abspath(filename)

print(path)
print(os.path.basename(path))
print(os.path.dirname(path))
print(os.path.splitext(path))
print(os.path.split(path))

readme_path = os.path.join(
os.path.dirname(path), '..', '..', 'README.rst')

print(readme_path)
print(os.path.normpath(readme_path))

Reading the result is probably a good enough explanation for this simple example:

/Users/fab/srv/lpp/ch7/files/fear.txt           # path
fear.txt # basename
/Users/fab/srv/lpp/ch7/files # dirname
('/Users/fab/srv/lpp/ch7/files/fear', '.txt') # splitext
('/Users/fab/srv/lpp/ch7/files', 'fear.txt') # split
/Users/fab/srv/lpp/ch7/files/../../README.rst # readme_path
/Users/fab/srv/lpp/README.rst # normalized
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset