Question
Code error where list index is out of range when doing command like python3 edfs.py -create /user/file Trying to follow instruction of python edfs.py -create
Code error where list index is out of range when doing command like "python3 edfs.py -create /user/file" Trying to follow instruction of python edfs.py -create which will create a file in the specified path. Note the file is empty and your implementation only needs to add the file name to your emulated file system. No need to add data for the file. The command should report an error if the file already exists. Also command "python3 edfs.py -ls /" should list all files under directory but is unable to due to some error that list object has no attribute keys.
Code transcript below, indents not here:
import sys
import json
import requests
import base64
# Firebase URL
firebase_url = "YOUR_FIREBASE_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 data is not None:
for key in data.keys():
if key == "content":
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))
# Function to create a new directory
def create_directory(dir_path):
data = {}
response = requests.put(firebase_url + dir_path + ".json", data=json.dumps(data))
# Function to remove a file or directory
def remove(path):
response = requests.delete(firebase_url + path + ".json")
# Function to export the file system structure in XML format
def export_xml():
root_data = get_firebase_data(firebase_url + ".json")
print("")
for key in root_data.keys():
print("")
export_directory(key)
print("" + key + ">")
print("")
# 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 data is not None:
for key in data.keys():
if key == "content":
content = data[key]
print("
else:
print("
export_directory(dir_path + "/" + key)
print("")
# Parse the command-line arguments
args = sys.argv
command = args[1]
path = args[2]
# Execute the appropriate command
if command == "-ls":
list_directory(path)
elif command == "-create":
content = args[3]
create_file(path, content)
elif command == "-mkdir":
create_directory(path)
elif command == "-rmdir" or command == "-rm":
remove(path)
elif command == "-export":
export_xml()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started