os.path
os.path
is a module in the standard library of Python that provides functions for interacting with the file system. The os.path
module provides a wide range of functions to work with file paths, check file existence, manipulate file and directory names, etc.
Some of the key features of the os.path
module include:
- Compatibility with both Windows and Unix file systems
- Support for both absolute and relative paths
- Support for various file system operations such as checking file existence, creating directories, etc.
- Access to various file and directory properties such as file size, modification time, etc.
You can use various functions of the os.path
module to work with file paths and perform file system operations.
Example:
import os
# Check if a file exists
file_path = '/path/to/file'
if os.path.exists(file_path):
print(f'{file_path} exists.')
else:
print(f'{file_path} does not exist.')
# Get the parent directory of a file
parent_dir = os.path.dirname(file_path)
print(f'Parent directory: {parent_dir}')
# Get the file name
file_name = os.path.basename(file_path)
print(f'File name: {file_name}')
The os.path
module is available in all versions of python and it is widely used for working with file system paths and perform various file system operations. It provides a lot of functionalities which are not present in pathlib
or vice-versa.
pathlib.Path
pathlib.Path
is a class in the pathlib
module in Python that provides an object-oriented way of interacting with file system paths. The Path
class allows you to easily manipulate file paths, create new directories, check file existence, and perform other common file system operations.
Some of the key features of the Path
class include:
- Compatibility with both Windows and Unix file systems
- Support for both absolute and relative paths
- Support for both bytes and text file paths
- Easy manipulation of file paths using string operations
- Support for multiple file system operations such as creating directories, checking file existence, etc.
You can create a Path
object by calling the Path()
constructor, passing it a string representing the path you want to work with. You can then use various methods and properties of the Path
object to interact with the file system.
from pathlib import Path
# Create a Path object
path = Path('/path/to/file')
# Check if the path exists
if path.exists():
print(f'{path} exists.')
else:
print(f'{path} does not exist.')
# Get the parent directory of the path
parent_dir = path.parent
print(f'Parent directory: {parent_dir}')
# Get the file name of the path
file_name = path.name
print(f'File name: {file_name}')
The pathlib
module is introduced in python 3.4 and it’s more powerful and easy to use than os.path
module.
Special Case:
train_data_dir = pathlib.Path('/home/ubuntu/myfolder/train/images/') / ('train')
The above code creates a Path
object for the directory ‘/home/ubuntu/myfolder/train/images/train/’. It uses the /
operator to concatenate two paths:
pathlib.Path('/home/ubuntu/myfolder/train/images/')
– creates aPath
object for the directory ‘/home/ubuntu/myfolder/train/images/’('train')
– is a string representing the directory name ‘train’
When you use the /
operator on a Path
object, it creates a new Path
object that is the concatenation of the original path and the passed string. So, in this case, it creates a new Path
object that points to the directory ‘/home/ubuntu/myfolder/train/images/train/’.
You can use this Path
object to perform various file system operations, such as checking if the directory exists, creating the directory if it doesn’t exist, or listing the files in the directory.
if not train_data_dir.exists():
train_data_dir.mkdir(parents=True)
print(f'{train_data_dir} exists: {train_data_dir.exists()}')
This will check if the directory train_data_dir
exists, if not it will create it along with its parent directories.