The path.join method of the os module

The path.join method combines paths or their parts specified in the method parameter. The combination is performed using a separator specific to the used OS. For Windows, the separator \ is used, for Linux - /. If the last path component to be combined is empty, then the separator is placed at the end again. If the path component is an absolute path, then all previously combined components are discarded, and the combination continues from the absolute path component. The method returns a string in which all specified paths are combined.

Syntax

pythonimport os

print(os.path.join(parts of the way))

Example

Let's combine several paths:

pythonimport os

print(os.path.join('dir', 'dir1', 'file.txt'))

The result of the executed code:

pythondir\dir1\file.txt

See also

  • method path.exists of module os,
    which checks the existence of a path
  • method path.isdir of module os,
    which checks the existence of a folder
  • method path.isfile of module os,
    which checks the existence of a file
  • method path.getsize of module os,
    which returns the size of the path in bytes
  • method stat of module os,
    which returns the status of the path