The code is unable to add multiple files to the same directory or add new directories under an existing one. It replaces existing files instead of adding new ones. Directories can only be created with a trailing slash. The code uses PUT requests instead of POST which may be causing the issues. The user asks how to fix these problems.
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
My code does not work right in that when i run create_file- i am unabl.pdf
1. My code does not work right in that when i run create_file, i am unable to keep adding files to
the same directory as it just replaces the file instead. I tried changing the code to use .post instead
of .put but it only adds some file with random letters and numbers instead of the name of the file
I am trying to add in. The same thing happens when I am trying to add new directories under
another directory. I can also only make a standalone directory after adding a "/" afterward ie.
(python3 edfs.py -mkdir test/) while (python3 edfs.py -mkdir list) does not work. How can I fix
these issues.
Here is my code transcript:
import sys
import json
import requests
import base64
# Firebase URL
firebase_url = "url"
# Function to get the data from Firebase
def get_firebase_data(url):
response = requests.get(url)
data = json.loads(response.text)
return data
# Function to list all files and directories under a given directory
def list_directory(dir_path):
data = get_firebase_data(firebase_url + dir_path + ".json")
if isinstance(data, dict):
for key in data.keys():
if key == "content":
2. print(dir_path + ": " + data[key])
else:
list_directory(dir_path + "/" + key)
# Function to create a new file with the given content
def create_file(file_path, content):
data = {"content": content}
response = requests.put(firebase_url + file_path + ".json", data=json.dumps(data))
if response.status_code == 200:
print("File created successfully")
elif response.status_code == 400:
print("File already exists")
# Function to create a new directory
def create_directory(dir_path):
data = {dir_path: content}
response = requests.put(firebase_url + dir_path + ".json",data=json.dumps(data))
if response.status_code == 200:
print("Directory created successfully")
elif response.status_code == 400:
print("Directory already exists")
# Function to remove a file or directory
def remove(path):
response = requests.delete(firebase_url + path + ".json")
if response.status_code == 200:
3. print("File/directory removed successfully")
elif response.status_code == 404:
print("File/directory not found")
# Function to export the file system structure in XML format
def export_xml():
root_data = get_firebase_data(firebase_url + ".json")
print("<root>")
for key in root_data.keys():
print("<" + key + ">")
export_directory(key)
print("</" + key + ">")
print("</root>")
# Function to export a directory and its contents in XML format
def export_directory(dir_path):
data = get_firebase_data(firebase_url + dir_path + ".json")
if isinstance(data, dict):
for key in data.keys():
if key == "content":
content = data[key]
print("<file name='" + dir_path + "'>" + base64.b64encode(content.encode('utf-8')).decode('utf-
8') + "</file>")
else:
print("<directory name='" + key + "'>")
export_directory(dir_path + "/" + key)
4. print("</directory>")
# Parse the command-line arguments
args = sys.argv
if len(args) < 3:
print("Usage: python3 edfs.py <command> <path> [content]")
exit()
command = args[1]
x= args[2].split('/')
content = x[-1]
del x[-1]
path='/'.join(x)
print(path)
# Use the extracted path and file name as needed
if command == "-create":
if len(args) < 2:
print("Usage: python3 edfs.py -create <path> <content>")
exit()
create_file(path, content)
elif command == "-mkdir":
create_directory(path)
elif command == "-rmdir" or command == "-rm":
remove(path)
elif command == "-ls":