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