I have a program that accepts a filename as a parameter from the user, as in:
$ myprog -i filename
The filename is a file containing JSON data.
When I try to load the file into a JSON object such as this:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-i', action='store')
filename = args.i
json_data = json.load(filename)
I am getting an error from Python saying:
line 316, in main
json_data = json.load(input_file)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/__init__.py", line 293, in load
return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'
I need to "tell" Python that the passed parameter is an actual file that json.load should consume, I am not sure how to do that?
"tell" Python that the passed parameter is an actual filebecause it isn't; it's the name of a file. So - how do you ordinarily get "an actual file", given the file name? For example, if you wanted to read from it or write to it directly with the file methods? (if you don't know these things, you should study fundamentals first before trying this project.)