Python os.path Module

Python os.path Module

  1. Syntax of Python os.path Module
  2. Important Methods of Python os.path Module

Python has an os module that helps users interact with the operating system and hardware with the help of various system calls and functions. The os module has several sub-modules.

One of them is the path module. The path module is packed with various methods dealing with pathnames.

Here, a pathname refers to a file system path of a resource.

Syntax of Python os.path Module

os.path.<function name>()

Here, <function name> must be replaced with a valid function name and () indicates that we are calling that function.

Parameters

Since the os.path module deals with file system paths, the parameter(s) for most of this module’s functions is a path, list, or tuple of paths. Every path can be either represented as a Python string or a Python bytes string.

Additionally, one method, join(), also accepts non-keyword arguments, which are also paths. This method joins all the paths together via a separator.

Returns

Most methods of the os.path module return a path. The returned value type depends upon the input or parameter value type, meaning a string for a string and a bytes string for a bytes string.

Moreover, some procedures such as exists(), isabs(), isfile(), and islink(), that essentially perform validation and verification tasks, return Boolean values.

Lastly, some methods such as split() and splitext() return a tuple and the ones employed with time calculations, such as getctime() and getmtime(), return a float value representing time in seconds.

Important Methods of Python os.path Module

The path module contains implementations for various utilities that are a part of most projects, which are robust and reliable. Some of the important ones are as follows.

Method Details Returns
os.path.abspath(path) This method returns a normalized abstract path of the received path. String
os.path.basename(path) This methods returns the basename of the received path. String
os.path.commonpath(paths) This method returns the longest common sub-path in the list of file system paths. String
os.path.commonprefix(list) This method returns the longest common prefix in the list of file system paths. String
os.path.dirname(path) This method returns the directory name in the received path. String
os.path.exists(path) This method checks if the received path exists or not. Boolean
os.path.getatime(path) This method returns the time when the path was last accessed in seconds. Float
os.path.getmtime(path) This method returns the time when the path was last changed in seconds. Float
os.path.getctime(path) This method returns the time when the metadata was last changed in seconds. Float
os.path.getsize(path) This method calculates the size of the received path in bytes. Integer
os.path.isabs(path) This method checks if the received path is an absolute file system path. Boolean
os.path.isfile(path) This method checks if the received path is an existing file. Boolean
os.path.isdir(path) This method checks if the received path is an existing directory. Boolean
os.path.islink(path) This method checks if the received path is an existing symbolic link. Boolean
os.path.join(path, *paths) This method joins multiple paths together with a separator, generally /. String
os.path.normcase(path) This method normalizes the received path by converting the case of all the characters to lowercase. String
os.path.normpath(path) This method normalizes the received path by removing redundant separators and characters. String
os.path.relpath(path, start = os.curdir) This method returns a relative file path for the received path from the start, where start is the starting directory. By default, it is set to the current directory. String
os.path.split(path) This method splits the received path into a pair of head and tail, where head is everything except the last component in the path and tail is the last component. In case of /users/vaibhav/games/rdr2.exe, /users/vaibhav/games is the head and rdr2 is the tail. A tuple of (head, tail).
os.path.splitext(path) This method splits the received path into a pair of root and ext. In other words, root + ext = path. A tuple of (root, ext). If no extension exists, ext will be an empty string.

To learn more about this module, refer to the official Python documentation here.

Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

LinkedIn GitHub

Related Article - Python OS

  • Python os.set_handle_inheritable() Method
  • Python os.set_inheritable() Method
  • Python os.stat_result Class
  • Python os.renames() Method
  • Python os.get_handle_inheritable Method
  • Python os.get_inheritable Method