Error in reading the data from the file as specified by --directory flag

I’m facing an error in reading the data from the file as specified by --directory flag. It seems I’m trying to read from file that doesn’t exist or I’m reading it incorrectly…? ss attached for the same.

Code in python that I’m using to read file from absolute path:
fn = sys.argv[2]
if os.path.exists(fn):
with open(os.path.basename(fn)) as f:
file_data = f.read()

@kunalghai2020 There are three issues at play here:

  1. sys.argv[2] (content of –directory) refers to a directory, not a file, so it can’t be opened as a file.

You can verify by listing the content of sys.argv[2] like this:

print('sys.argv[2] is a directory, and its contents are', os.listdir(sys.argv[2]))


  1. You need to wait for our tester to send a request, determine which file is being requested, and then read that file from the sys.argv[2] directory.


  1. os.path.basename isn’t the appropriate function for this task. Instead, you’ll need to use os.path.join to construct the correct file path.

Thanks @andy1li for the help. I now realize that I had misunderstood the instruction & my code was incorrect.

PS: Your solution worked and all tests for #AP6 Return a file passed.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.