The datamol.fs module provides high-level functions for manipulating files and directories. While the examples below use local paths, all functions support remote protocols such as S3, GCS, HTTP, FTP, and Git because the module is built on top of fsspec.
Key destructive operations include:
mkdir(path, exist_ok=True): Creates a directory.copy_file(source, destination, progress=True, force=True): Copies a single file. Use force=True to overwrite existing files.copy_dir(source, destination, progress=True): Copies an entire directory tree.
import datamol as dm
# Create a directory
subdir_path = dm.fs.join(temp_dir, "subdir1", "subsubdir293")
dm.fs.mkdir(subdir_path, exist_ok=True)
# Copy a single file from a URL to a local path
destination_path = dm.fs.join(subdir_path, "cdk2.sdf")
dm.fs.copy_file(
source="https://raw.githubusercontent.com/rdkit/rdkit/master/Docs/Book/data/cdk2.sdf",
destination=destination_path,
progress=True,
force=True,
)
# Copy a full directory tree
subdir2_path = dm.fs.join(temp_dir, "subdir2")
dm.fs.copy_dir(
source="https://ftp.ncbi.nlm.nih.gov/pubchem/specifications/",
destination=subdir2_path,
progress=True,
)