I'm a first year student in CS, attempting to debug a simple Python script.
The script is attempting to parse a directory of JSON files, aka an AWS bucket. I can't figure out where these errors come from, however:
import json
import os
from pprint import pprint
jsonDirectory = "/path/to/dir/"
targetRegion = "-insert-region-here"
print("Searching for records with AWS Region: " + targetRegion)
print("")
for filename in os.listdir(jsonDirectory):
print("Reading: " + filename)
data = json.dumps(open(jsonDirectory + filename))
for i in range(len(data["Records"])):
if data["Records"][i]["awsRegion"] == targetRegion:
print("---------------------------")
print("Record #" + str(i))
print("Username: " + data["Records"][i]["userIdentity"] ["userName"])
print("Event name: " + data["Records"][i]["eventName"])
print("Event time: " + data["Records"][i]["eventTime"])
print("---------------------------")
print("")
print("Completed reading files.")
The errors:
Traceback (most recent call last): File "/path/to/file.py", line 13, in data = json.dumps(open(jsonDirectory + filename)) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/init.py", line 231, in dumps return _default_encoder.encode(obj) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 257, in iterencode return _iterencode(o, 0) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 180, in default o.class.name) TypeError: Object of type 'TextIOWrapper' is not JSON serializable
json.loadinstead ofjson.dumps- YGouddi