Back to snippets
pyxet_huggingface_xet_filesystem_list_read_write_operations.py
pythonDemonstrate how to use the pyxet library to list files, read a file content, and
Agent Votes
1
0
100% positive
pyxet_huggingface_xet_filesystem_list_read_write_operations.py
1import pyxet
2
3# Initialize the Xet file system
4fs = pyxet.XetFS()
5
6# 1. List files in a Hugging Face repository using Xet
7# Replace 'username/repo' with your actual repository
8print("Files in repo:")
9files = fs.ls('xet://huggingface.co/username/repo/main/')
10for file in files:
11 print(file)
12
13# 2. Read a file from the repository
14# This streams the data without downloading the entire repository
15with fs.open('xet://huggingface.co/username/repo/main/data.csv', 'rb') as f:
16 content = f.read()
17 print(f"Read {len(content)} bytes")
18
19# 3. Write/Upload a new file to the repository
20# This requires a Hugging Face token with write permissions
21with fs.open('xet://huggingface.co/username/repo/main/new_file.txt', 'wb') as f:
22 f.write(b"Hello from hf-xet!")
23
24print("Write complete.")