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
import os
print(os.path.join(parts of the way))
Example
Let's combine several paths:
import os
print(os.path.join('dir', 'dir1', 'file.txt'))
The result of the executed code:
dir\dir1\file.txt
See also
-
method
path.existsof moduleos,
which checks the existence of a path -
method
path.isdirof moduleos,
which checks the existence of a folder -
method
path.isfileof moduleos,
which checks the existence of a file -
method
path.getsizeof moduleos,
which returns the size of the path in bytes -
method
statof moduleos,
which returns the status of the path