6

Why does this return AttributeError: __enter__

Sorting method is just a string created based on how the list is sorted, and current time uses stfttime

current_time = strftime("%Y-%m-%d %H-%M-%S", gmtime())

filename = f"Komplett-{str(sorting_method)}-{str(current_time)}.txt"
if not os.path.exists(f'C:/Users/tagp/OneDrive/Dokumenter/Python/{filename}'):
        open(str(filename), "w+")   
with (filename, "w+") as json_data:
            my_list = {}
            my_list["products"] = []
            for thing in my_products:
                my_list["products"].append({
                    "Product Title":thing.title,
                    "Price":thing.price,
                    "Rating":thing.rating,
                    "Stock":thing.stock
                    })
            json.dump(my_list, json_data, indent = 4)

Full traceback:

Traceback (most recent call last):
    File "komplett.py", line 172, in <module>
        with (filename, "w") as json_data:
AttributeError: __enter__
4
  • can you post the complete traceback Commented Sep 18, 2017 at 6:51
  • Edited post with full traceback Commented Sep 18, 2017 at 7:00
  • 4
    You forgot the open. Commented Sep 18, 2017 at 7:00
  • That's it! Thanks Commented Sep 18, 2017 at 7:04

1 Answer 1

8

You just for got to use open

current_time = strftime("%Y-%m-%d %H-%M-%S", gmtime())

filename = f"Komplett-{str(sorting_method)}-{str(current_time)}.txt"
if not os.path.exists(f'C:/Users/tagp/OneDrive/Dokumenter/Python/{filename}'):
        open(str(filename), "w+")   
with open(filename, "w+") as json_data:
            my_list = {}
            my_list["products"] = []
            for thing in my_products:
                my_list["products"].append({
                    "Product Title":thing.title,
                    "Price":thing.price,
                    "Rating":thing.rating,
                    "Stock":thing.stock
                    })
            json.dump(my_list, json_data, indent = 4)
Sign up to request clarification or add additional context in comments.

2 Comments

Welcome, and also dont forget to post the traceback when posting a question. it will be easier for the community peers to solve the query
I figured it might have been somewhat easy to understand what it was about, given how short it was. My mistake though will remember that for next time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.