Migrating from OS.PATH to PATHLIB Module in Python
Learn how to use the modern pathlib module to perform tasks you have been using os.path for
In this article, I will go over the most frequent tasks related to file paths and show how you can refactor the old approach of using os.path module to the new cleaner way using pathlib module.
Joining paths
import os
= '/home/ubuntu/'
base_path = 'data.csv'
filename os.path.join(base_path, filename)
In pathlib, we can use the division operator to separate the paths elegantly.
from pathlib import Path
= '/home/ubuntu/'
base_path = 'data.csv'
filename / filename Path(base_path)
Get absolute path
import os
__file__) os.path.abspath(
from pathlib import Path
__file__).resolve() Path(
Get current working directory
import os
os.getcwd()
from pathlib import Path
Path.cwd()
Check if path is a file
import os
'/home/ubuntu/data.csv') os.path.isfile(
from pathlib import Path
'/home/ubuntu/data.csv').is_file() Path(
Check if path is a directory
import os
'/home/ubuntu/') os.path.isdir(
from pathlib import Path
'/home/ubuntu/').is_dir() Path(
Check if a path exists
import os
'/home/ubuntu/') os.path.exists(
from pathlib import Path
'/home/ubuntu/').exists() Path(
Get path to folder containing a file
import os
'/home/ubuntu/data.csv')
os.path.dirname(# /home/ubuntu
from pathlib import Path
'/home/ubuntu/data.csv').parent
Path(# /home/ubuntu
Get the path to the home directory
import os
'~') os.path.expanduser(
from pathlib import Path
Path.home()
Expand the user home directory in a path
import os
'~/Desktop')
os.path.expanduser(# '/home/ubuntu/Desktop'
from pathlib import Path
'~/Desktop').expanduser() Path(
Get size in bytes of a file
import os
'/home/ubuntu/data.csv') os.path.getsize(
from pathlib import Path
'/home/ubuntu/data.csv').stat().st_size Path(
Get file extension
import os
= os.path.splitext('/home/ubuntu/hello.py')
path, ext # ('/home/ubuntu/hello', '.py')
from pathlib import Path
'/home/ubuntu/hello.py').suffix
Path(# .py
Change permission of a file
import os
'key.pem', 0o400) os.chmod(
from pathlib import Path
'key.pem').chmod(0o400) Path(
Get file name without directory
import os
'/home/ubuntu/hello.py')
os.path.basename(# hello.py
from pathlib import Path
'/home/ubuntu/hello.py').name
Path(# hello.py
List contents of a directory
import os
os.listdir()
from pathlib import Path
Path().iterdir()
Create a directory
import os
'/home/ubuntu/data', exist_ok=True) os.makedirs(
from pathlib import Path
'/home/ubuntu/data').mkdir(exist_ok=True) Path(
Rename files or directories
import os
'rows.csv', 'data.csv') os.rename(
from pathlib import Path
'rows.csv').rename('data.csv') Path(
Delete a directory
import os
'/home/ubuntu/test') os.rmdir(
from pathlib import Path
'/home/ubuntu/test').rmdir() Path(
Reading a file
import os
= os.path.join('/home/ubuntu', 'data.csv')
p
with open(p) as fp:
= fp.read() data
In new versions of python, you can directly pass a pathlib Path
to the open()
function.
from pathlib import Path
= Path('/home/ubuntu/') / 'data.csv'
path
with open(path) as fp:
= fp.read() data
In older versions, you can either convert the path to a string using str()
or use the open()
method.
from pathlib import Path
= Path('/home/ubuntu/data.csv')
path
# Method: 1
= path.open().read()
data
# Method 2
with open(str(path)) as fp:
= fp.read()
data
# Method 3
= path.read_text() data