Spaces:
Sleeping
Sleeping
File size: 1,832 Bytes
efcee1c a441422 efcee1c a441422 efcee1c 2f0d72d efcee1c 782e3c8 efcee1c a62fe03 efcee1c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
from huggingface_hub import HfFileSystem
def manage_hf_files(hf_token):
# Initialize the Hugging Face file system
fs = HfFileSystem(token = hf_token)
try:
# List all files in a directory
print("Listing all files in the directory:")
files_in_dir = fs.ls("datasets/bupa1018/Test/data", detail=False)
for file in files_in_dir:
print(file)
# List all ".csv" files in a repository
print("\nListing all '.csv' files in the repository:")
csv_files = fs.glob("datasets/bupa1018/Test/**/*.csv")
for csv_file in csv_files:
print(csv_file)
print("\nListing all '.csv' files in the repository:")
json_files = fs.glob("datasets/bupa1018/Test/**/*.json")
for json_file in json_files:
print("Json_File:", json_file)
# Read a remote file line-by-line
print("\nReading the content of 'repo_data.json' (line-by-line):")
with fs.open("datasets/bupa1018/Test/data/repo_data.json", "r") as f:
repo_data = f.readlines()
for line in repo_data:
print(line.strip())
# Read the content of a remote file as a single string
print("\nReading the content of 'repo_data.json' (as a single string):")
train_data = fs.read_text("datasets/bupa1018/Test/data/repo_data.json")
print("print the content of a remote file as a single string")
print(train_data)
# Write to a remote filehd
print("\nWriting to 'hello.csv':")
with fs.open("datasets/bupa1018/Test/data/hello2.csv", "w") as f:
f.write("text,label\n")
f.write("Fantastic movie!,good\n")
print("Data successfully written to 'hello.csv'.")
except Exception as e:
print(f"An error occurred: {e}")
|