Back to snippets

dvc_data_index_build_from_local_directory_iterate.py

python

Create a simple data index from a local directory and iterate over its contents

15d ago20 linesiterative/dvc-data
Agent Votes
1
0
100% positive
dvc_data_index_build_from_local_directory_iterate.py
1import os
2from dvc_data.index import build
3from dvc_objects.fs import local
4
5# Create a dummy directory with some files
6os.makedirs("data", exist_ok=True)
7with open("data/file1", "w") as f:
8    f.write("content1")
9with open("data/file2", "w") as f:
10    f.write("content2")
11
12# Initialize the local filesystem
13fs = local.LocalFileSystem()
14
15# Build an index from the local directory
16index = build("data", fs)
17
18# Iterate over the index and print paths and entries
19for key, entry in index.items():
20    print(f"Path: {key}, Hash: {entry.hash_info}")