bupa1018's picture
Update test.py
a62fe03
raw
history blame
1.83 kB
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}")