DEV Community

Amit Chaudhary
Amit Chaudhary

Posted on • Edited on • Originally published at amitness.com

Migrating from OS.PATH to PATHLIB Module in Python

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
base_path = '/home/ubuntu/'
filename = 'data.csv'
os.path.join(base_path, filename)
Enter fullscreen mode Exit fullscreen mode

In pathlib, we can use the division operator to separate the paths elegantly.

from pathlib import Path
base_path = '/home/ubuntu/'
filename = 'data.csv'
Path(base_path) / filename
Enter fullscreen mode Exit fullscreen mode

Get absolute path

import os
os.path.abspath(__file__)
Enter fullscreen mode Exit fullscreen mode
from pathlib import Path
Path(__file__).resolve()
Enter fullscreen mode Exit fullscreen mode

Get current working directory

import os
os.getcwd()
Enter fullscreen mode Exit fullscreen mode
from pathlib import Path
Path.cwd()
Enter fullscreen mode Exit fullscreen mode

Check if path is a file

import os
os.path.isfile('/home/ubuntu/data.csv')
Enter fullscreen mode Exit fullscreen mode
from pathlib import Path
Path('/home/ubuntu/data.csv').is_file()
Enter fullscreen mode Exit fullscreen mode

Check if path is a directory

import os
os.path.isdir('/home/ubuntu/')
Enter fullscreen mode Exit fullscreen mode
from pathlib import Path
Path('/home/ubuntu/').is_dir()
Enter fullscreen mode Exit fullscreen mode

Check if a path exists

import os
os.path.exists('/home/ubuntu/')
Enter fullscreen mode Exit fullscreen mode
from pathlib import Path
Path('/home/ubuntu/').exists()
Enter fullscreen mode Exit fullscreen mode

Get path to folder containing a file

import os
os.path.dirname('/home/ubuntu/data.csv')
# /home/ubuntu
Enter fullscreen mode Exit fullscreen mode
from pathlib import Path
Path('/home/ubuntu/data.csv').parent
# /home/ubuntu
Enter fullscreen mode Exit fullscreen mode

Get the path to the home directory

import os
os.path.expanduser('~')
Enter fullscreen mode Exit fullscreen mode
from pathlib import Path
Path.home()
Enter fullscreen mode Exit fullscreen mode

Expand the user home directory in a path

import os
os.path.expanduser('~/Desktop')
# '/home/ubuntu/Desktop'
Enter fullscreen mode Exit fullscreen mode
from pathlib import Path
Path('~/Desktop').expanduser()
Enter fullscreen mode Exit fullscreen mode

Get size in bytes of a file

import os
os.path.getsize('/home/ubuntu/data.csv')
Enter fullscreen mode Exit fullscreen mode
from pathlib import Path
Path('/home/ubuntu/data.csv').stat().st_size
Enter fullscreen mode Exit fullscreen mode

Get file extension

import os
path, ext = os.path.splitext('/home/ubuntu/hello.py')
# ('/home/ubuntu/hello', '.py')
Enter fullscreen mode Exit fullscreen mode
from pathlib import Path
Path('/home/ubuntu/hello.py').suffix
# .py
Enter fullscreen mode Exit fullscreen mode

Change permission of a file

import os
os.chmod('key.pem', 0o400)
Enter fullscreen mode Exit fullscreen mode
from pathlib import Path
Path('key.pem').chmod(0o400)
Enter fullscreen mode Exit fullscreen mode

Get file name without directory

import os
os.path.basename('/home/ubuntu/hello.py')
# hello.py
Enter fullscreen mode Exit fullscreen mode
from pathlib import Path
Path('/home/ubuntu/hello.py').name
# hello.py
Enter fullscreen mode Exit fullscreen mode

List contents of a directory

import os
os.listdir()
Enter fullscreen mode Exit fullscreen mode
from pathlib import Path
Path().iterdir()
Enter fullscreen mode Exit fullscreen mode

Create a directory

import os
os.makedirs('/home/ubuntu/data', exist_ok=True)
Enter fullscreen mode Exit fullscreen mode
from pathlib import Path
Path('/home/ubuntu/data').mkdir(exist_ok=True)
Enter fullscreen mode Exit fullscreen mode

Rename files or directories

import os
os.rename('rows.csv', 'data.csv')
Enter fullscreen mode Exit fullscreen mode
from pathlib import Path
Path('rows.csv').rename('data.csv')
Enter fullscreen mode Exit fullscreen mode

Delete a directory

import os
os.rmdir('/home/ubuntu/test')
Enter fullscreen mode Exit fullscreen mode
from pathlib import Path
Path('/home/ubuntu/test').rmdir()
Enter fullscreen mode Exit fullscreen mode

Reading a file

import os
p = os.path.join('/home/ubuntu', 'data.csv')

with open(p) as fp:
    data = fp.read()
Enter fullscreen mode Exit fullscreen mode

In new versions of python, you can directly pass a pathlib Path to the open() function.

from pathlib import Path
path = Path('/home/ubuntu/') / 'data.csv'

with open(path) as fp:
    data = fp.read()
Enter fullscreen mode Exit fullscreen mode

In older versions, you can either convert the path to a string using str() or use the open() method.

from pathlib import Path
path = Path('/home/ubuntu/data.csv')

# Method: 1
data = path.open().read()

# Method 2
with open(str(path)) as fp:
    data = fp.read()

# Method 3
data = path.read_text()
Enter fullscreen mode Exit fullscreen mode

Connect

If you enjoyed this blog post, feel free to connect with me on Twitter where I share new blog posts every week.

Top comments (0)