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)
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
Get absolute path
import os
os.path.abspath(__file__)
from pathlib import Path
Path(__file__).resolve()
Get current working directory
import os
os.getcwd()
from pathlib import Path
Path.cwd()
Check if path is a file
import os
os.path.isfile('/home/ubuntu/data.csv')
from pathlib import Path
Path('/home/ubuntu/data.csv').is_file()
Check if path is a directory
import os
os.path.isdir('/home/ubuntu/')
from pathlib import Path
Path('/home/ubuntu/').is_dir()
Check if a path exists
import os
os.path.exists('/home/ubuntu/')
from pathlib import Path
Path('/home/ubuntu/').exists()
Get path to folder containing a file
import os
os.path.dirname('/home/ubuntu/data.csv')
# /home/ubuntu
from pathlib import Path
Path('/home/ubuntu/data.csv').parent
# /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
os.path.expanduser('~/Desktop')
# '/home/ubuntu/Desktop'
from pathlib import Path
Path('~/Desktop').expanduser()
Get size in bytes of a file
import os
os.path.getsize('/home/ubuntu/data.csv')
from pathlib import Path
Path('/home/ubuntu/data.csv').stat().st_size
Get file extension
import os
path, ext = os.path.splitext('/home/ubuntu/hello.py')
# ('/home/ubuntu/hello', '.py')
from pathlib import Path
Path('/home/ubuntu/hello.py').suffix
# .py
Change permission of a file
import os
os.chmod('key.pem', 0o400)
from pathlib import Path
Path('key.pem').chmod(0o400)
Get file name without directory
import os
os.path.basename('/home/ubuntu/hello.py')
# hello.py
from pathlib import Path
Path('/home/ubuntu/hello.py').name
# hello.py
List contents of a directory
import os
os.listdir()
from pathlib import Path
Path().iterdir()
Create a directory
import os
os.makedirs('/home/ubuntu/data', exist_ok=True)
from pathlib import Path
Path('/home/ubuntu/data').mkdir(exist_ok=True)
Rename files or directories
import os
os.rename('rows.csv', 'data.csv')
from pathlib import Path
Path('rows.csv').rename('data.csv')
Delete a directory
import os
os.rmdir('/home/ubuntu/test')
from pathlib import Path
Path('/home/ubuntu/test').rmdir()
Reading a file
import os
p = os.path.join('/home/ubuntu', 'data.csv')
with open(p) as fp:
data = fp.read()
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()
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()
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)