Python: Get Dir Path, File Name Part, File Name Extension, Absolute Path, Check File Existence …

,

This page is a list of the most frequently used functions for manipulating file/directory path.

Get Directory Part, File Name Part, File Extension

Get Directory Path

os.path.dirname("path") β†’ returns the dir part of path. (result won't have ending slash, unless it's root dir.)

# -*- coding: utf-8 -*-
# python

# get dir part

import os.path

print os.path.dirname("/home/aa/bb/ff.html")
# /home/aa/bb

10.1. os.path β€” Common pathname manipulations β€” Python v2.7.6 documentation #os.path.dirname

Get File Name Part of a Path

# -*- coding: utf-8 -*-
# python

# get file name part of a path

import os.path

print os.path.basename("/home/aa/bb/ff.html")
# 'ff.html'

10.1. os.path β€” Common pathname manipulations β€” Python v2.7.6 documentation #os.path.basename

Split Directory Part, File Part

os.path.split("path") β†’ returns a 2-tuple. Split a path into dir part and file part. (note: result of dir part don't have ending slash, unless it's root dir.)

# -*- coding: utf-8 -*-
# python

# split path into dir part, file part

import os.path

fp = "/home/aa/bb/ff.html"

print os.path.split(fp)
# ('/home/aa/bb', 'ff.html')

print os.path.dirname(fp)
# /home/aa/bb

print os.path.basename(fp)
# ff.html

10.1. os.path β€” Common pathname manipulations β€” Python v2.7.6 documentation #os.path.split

Note: os.path.split(p) is equal to os.path.dirname(p) + os.path.basename(p)

Get File Extension

os.path.splitext("path") β†’ returns a 2-tuple. Split a path into first part and file extension part.

# -*- coding: utf-8 -*-
# python

# get file extension

import os.path

print os.path.splitext("/home/aa/bb/ff.html")
# ('/home/aa/bb/ff', '.html')

print os.path.splitext("/home/aa/bb/ff.html")[1]
# '.html'

10.1. os.path β€” Common pathname manipulations β€” Python v2.7.6 documentation #os.path.splitext

Check File Exists

os.path.exists(path) β†’ Return True if the file exists, else False.

# -*- coding: utf-8 -*-
# python

# check if file exists

import os.path

print os.path.exists("/home/") # True

print os.path.exists("/home/m31455") # False

10.1. os.path β€” Common pathname manipulations β€” Python v2.7.6 documentation #os.path.exists

Get Relative Path

os.path.relpath(path, dir) β†’ returns a relative path, of path with respect to dir. (Note: path existence is not checked.)

# -*- coding: utf-8 -*-
# python

# compute relative path

import os.path

print os.path.relpath("/home/aa/bb/ff.html", "/home/aa/") # bb/ff.html

10.1. os.path β€” Common pathname manipulations β€” Python v2.7.6 documentation #os.path.relpath

Absolute Path, Normalized/Cannonical Path

os.path.abspath(path) β†’ Return a normalized absolutized version of path. Note: it does not resolve symbolic links to its destination.

# -*- coding: utf-8 -*-
# python

# get absolute path

import os.path

fp = "/usr/bin/../bin/vi"

# remove redundant ../
print os.path.normpath(fp)
# /usr/bin/vi

# similar to os.path.normpath(fp) but does a bit more
print os.path.abspath(fp)
# /usr/bin/vi

Real Path, Resolve Symbolic Link

os.path.realpath(path) β†’ Return the canonical path, resolve symbolic links.

# -*- coding: utf-8 -*-
# python

# resolve symbolic link

import os.path

print os.path.realpath("/usr/bin/vi")
# /usr/bin/vim.basic

print os.path.realpath("/usr/bin/../bin/vi")
# /usr/bin/vim.basic

10.1. os.path β€” Common pathname manipulations β€” Python v2.7.6 documentation #os.path.realpath

blog comments powered by Disqus