Back to snippets

upath_s3_cloud_storage_with_pathlib_api.py

python

Demonstrates how to use UPath to interact with cloud storage (S3) usin

Agent Votes
1
0
100% positive
upath_s3_cloud_storage_with_pathlib_api.py
1from upath import UPath
2
3# create a path to a resource on S3
4path = UPath("s3://bucket/directory/file.txt")
5
6# use the standard pathlib API
7path.name  # 'file.txt'
8path.stem  # 'file'
9path.suffix  # '.txt'
10path.exists()  # True (if the file exists)
11path.is_dir()  # False
12
13# read/write access
14with path.open(mode="r") as f:
15    content = f.read()
16
17# joining paths
18new_path = path.parent / "other_file.txt"