os.path

The os.path module is used to manipulate pathnames in a portable manner. It’s imported by the os module.

abspath(path)

Returns an absolute version of the pathname path, taking the current working directory into account. For example, abspath('../Python/foo') might return '/home/beazley/Python/foo'.

basename(path)

Returns the base name of pathname path. For example, basename('/usr/local/python') returns 'python'.

commonprefix(list)

Returns the longest string that’s a prefix of all strings in list. If list is empty, the empty string is returned.

dirname(path)

Returns the directory name of pathname path. For example, dirname('/usr/local/python') returns '/usr/local'.

exists(path)

Returns True if path refers to an existing path. Returns

Get Python: Essential Reference, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.