SlideShare a Scribd company logo
1 of 4
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("<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 data is not None:
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)
print("</directory>")
# 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()

More Related Content

Similar to Code error where list index is out of range when doing command like -p.docx

C# Application program UNIT III
C# Application program UNIT IIIC# Application program UNIT III
C# Application program UNIT III
Minu Rajasekaran
 
12-OO-PHP.pptx
12-OO-PHP.pptx12-OO-PHP.pptx
12-OO-PHP.pptx
rani marri
 
4 - Files and Directories - Pemrograman Internet Lanjut.pptx
4 - Files and Directories - Pemrograman Internet Lanjut.pptx4 - Files and Directories - Pemrograman Internet Lanjut.pptx
4 - Files and Directories - Pemrograman Internet Lanjut.pptx
MasSam13
 
Recursively Searching Files and DirectoriesSummaryBuild a class .pdf
Recursively Searching Files and DirectoriesSummaryBuild a class .pdfRecursively Searching Files and DirectoriesSummaryBuild a class .pdf
Recursively Searching Files and DirectoriesSummaryBuild a class .pdf
mallik3000
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappers
Positive Hack Days
 
Web2py Code Lab
Web2py Code LabWeb2py Code Lab
Web2py Code Lab
Colin Su
 
CodeIgniter Helper Functions
CodeIgniter Helper FunctionsCodeIgniter Helper Functions
CodeIgniter Helper Functions
Jamshid Hashimi
 

Similar to Code error where list index is out of range when doing command like -p.docx (20)

File System in Nodejs.pdf
File System in Nodejs.pdfFile System in Nodejs.pdf
File System in Nodejs.pdf
 
C# Application program UNIT III
C# Application program UNIT IIIC# Application program UNIT III
C# Application program UNIT III
 
12-OO-PHP.pptx
12-OO-PHP.pptx12-OO-PHP.pptx
12-OO-PHP.pptx
 
Scala Slick-2
Scala Slick-2Scala Slick-2
Scala Slick-2
 
4 - Files and Directories - Pemrograman Internet Lanjut.pptx
4 - Files and Directories - Pemrograman Internet Lanjut.pptx4 - Files and Directories - Pemrograman Internet Lanjut.pptx
4 - Files and Directories - Pemrograman Internet Lanjut.pptx
 
Files nts
Files ntsFiles nts
Files nts
 
Recursively Searching Files and DirectoriesSummaryBuild a class .pdf
Recursively Searching Files and DirectoriesSummaryBuild a class .pdfRecursively Searching Files and DirectoriesSummaryBuild a class .pdf
Recursively Searching Files and DirectoriesSummaryBuild a class .pdf
 
Processing files sequentially in mule
Processing files sequentially in muleProcessing files sequentially in mule
Processing files sequentially in mule
 
SphinxSE with MySQL
SphinxSE with MySQLSphinxSE with MySQL
SphinxSE with MySQL
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappers
 
Web2py Code Lab
Web2py Code LabWeb2py Code Lab
Web2py Code Lab
 
CodeIgniter Helper Functions
CodeIgniter Helper FunctionsCodeIgniter Helper Functions
CodeIgniter Helper Functions
 
Tutorial on developing a Solr search component plugin
Tutorial on developing a Solr search component pluginTutorial on developing a Solr search component plugin
Tutorial on developing a Solr search component plugin
 
Intoduction on Playframework
Intoduction on PlayframeworkIntoduction on Playframework
Intoduction on Playframework
 
FIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxFIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptx
 
Local SQLite Database with Node for beginners
Local SQLite Database with Node for beginnersLocal SQLite Database with Node for beginners
Local SQLite Database with Node for beginners
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
Csphtp1 17
Csphtp1 17Csphtp1 17
Csphtp1 17
 
File Upload
File UploadFile Upload
File Upload
 
What's new in Python 3.11
What's new in Python 3.11What's new in Python 3.11
What's new in Python 3.11
 

More from Joe7Y7Nolany

More from Joe7Y7Nolany (20)

Compare and contrast the two images and record your responses below- f.docx
Compare and contrast the two images and record your responses below- f.docxCompare and contrast the two images and record your responses below- f.docx
Compare and contrast the two images and record your responses below- f.docx
 
Compute net cash provided by operating activities using the indirect m.docx
Compute net cash provided by operating activities using the indirect m.docxCompute net cash provided by operating activities using the indirect m.docx
Compute net cash provided by operating activities using the indirect m.docx
 
Complete the following paragraph to describe the diversity that exists.docx
Complete the following paragraph to describe the diversity that exists.docxComplete the following paragraph to describe the diversity that exists.docx
Complete the following paragraph to describe the diversity that exists.docx
 
Complete the following paragraph to describe soil profiles- Soil profi.docx
Complete the following paragraph to describe soil profiles- Soil profi.docxComplete the following paragraph to describe soil profiles- Soil profi.docx
Complete the following paragraph to describe soil profiles- Soil profi.docx
 
Complete the following statements- 1- Mature red blood cells are also.docx
Complete the following statements- 1- Mature red blood cells are also.docxComplete the following statements- 1- Mature red blood cells are also.docx
Complete the following statements- 1- Mature red blood cells are also.docx
 
CommonElements- You are given three sorted sequences A-B-C of $n--2$-.docx
CommonElements- You are given three sorted sequences A-B-C of $n--2$-.docxCommonElements- You are given three sorted sequences A-B-C of $n--2$-.docx
CommonElements- You are given three sorted sequences A-B-C of $n--2$-.docx
 
Cold climate species in the worlds extensive northern land masses Slow.docx
Cold climate species in the worlds extensive northern land masses Slow.docxCold climate species in the worlds extensive northern land masses Slow.docx
Cold climate species in the worlds extensive northern land masses Slow.docx
 
Companies that use IFRS- a- Are allowed to report property- plant- and.docx
Companies that use IFRS- a- Are allowed to report property- plant- and.docxCompanies that use IFRS- a- Are allowed to report property- plant- and.docx
Companies that use IFRS- a- Are allowed to report property- plant- and.docx
 
Comments in PHPi )Single Line Comments Comments that do not exceed the.docx
Comments in PHPi )Single Line Comments Comments that do not exceed the.docxComments in PHPi )Single Line Comments Comments that do not exceed the.docx
Comments in PHPi )Single Line Comments Comments that do not exceed the.docx
 
Climate has a direct influence on the key eclosystem processes of and.docx
Climate has a direct influence on the key eclosystem processes of and.docxClimate has a direct influence on the key eclosystem processes of and.docx
Climate has a direct influence on the key eclosystem processes of and.docx
 
CLINICAL QUESTIONS 1-List the nursing interventions for clients with l.docx
CLINICAL QUESTIONS 1-List the nursing interventions for clients with l.docxCLINICAL QUESTIONS 1-List the nursing interventions for clients with l.docx
CLINICAL QUESTIONS 1-List the nursing interventions for clients with l.docx
 
Circle the correct answer- 1- The teclnique of using a group of disks.docx
Circle the correct answer- 1- The teclnique of using a group of disks.docxCircle the correct answer- 1- The teclnique of using a group of disks.docx
Circle the correct answer- 1- The teclnique of using a group of disks.docx
 
complete step give answer Three different methods of teaching English.docx
complete step give answer  Three different methods of teaching English.docxcomplete step give answer  Three different methods of teaching English.docx
complete step give answer Three different methods of teaching English.docx
 
Class abstraction means--- Putting all data fields private and creatin.docx
Class abstraction means--- Putting all data fields private and creatin.docxClass abstraction means--- Putting all data fields private and creatin.docx
Class abstraction means--- Putting all data fields private and creatin.docx
 
Consider the Titanic dataset- summarized according to economic status.docx
Consider the Titanic dataset- summarized according to economic status.docxConsider the Titanic dataset- summarized according to economic status.docx
Consider the Titanic dataset- summarized according to economic status.docx
 
Classify each of the following based on the macroeconomic definitions.docx
Classify each of the following based on the macroeconomic definitions.docxClassify each of the following based on the macroeconomic definitions.docx
Classify each of the following based on the macroeconomic definitions.docx
 
Consider the relationship between a parasite and its host- There is pr (1).docx
Consider the relationship between a parasite and its host- There is pr (1).docxConsider the relationship between a parasite and its host- There is pr (1).docx
Consider the relationship between a parasite and its host- There is pr (1).docx
 
Consider the investment projects in the table below- all of which have.docx
Consider the investment projects in the table below- all of which have.docxConsider the investment projects in the table below- all of which have.docx
Consider the investment projects in the table below- all of which have.docx
 
Consider the following two independent investment opportunities that a (1).docx
Consider the following two independent investment opportunities that a (1).docxConsider the following two independent investment opportunities that a (1).docx
Consider the following two independent investment opportunities that a (1).docx
 
Consider the following code- inchuding the initial pareet process- how.docx
Consider the following code- inchuding the initial pareet process- how.docxConsider the following code- inchuding the initial pareet process- how.docx
Consider the following code- inchuding the initial pareet process- how.docx
 

Recently uploaded

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 

Code error where list index is out of range when doing command like -p.docx

  • 1. 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:
  • 2. 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("<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):
  • 3. 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("<file name='" + dir_path + "'>" + base64.b64encode(content.encode('utf-8')).decode('utf- 8') + "</file>") else: print("<directory name='" + key + "'>") export_directory(dir_path + "/" + key) print("</directory>") # 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":
  • 4. remove(path) elif command == "-export": export_xml()