相关文章推荐
直爽的牛肉面  ·  python/openpyxl/DataVa ...·  1 周前    · 
勤奋的鸭蛋  ·  python - Set up of ...·  昨天    · 
大力的长颈鹿  ·  python - Conda env ...·  昨天    · 
个性的骆驼  ·  typescript 上传文件 ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

how to solve : TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper

Ask Question here is my code... def save(self): print('Inventory saving') with open('Inventory.txt','w') as f: json.dump(self.pets,f) print('saved') def loads(self): print('inventory lading') if not os.path.exists('Inventory.txt'): print('skipping, nothing to load') return with open('Inventory.txt','r') as f: self.pets = json.loads(f) print('loaded...!!') Md Sydur Rahaman Sep 21, 2021 at 14:30 Welcome to StackOverflow! Please avoid uploading code as an image. meta.stackoverflow.com/questions/285551/… nima Sep 22, 2021 at 13:23

You are using loads when you need load . json.load is for file-like objects, and json.loads is for strings. (You could also load the string into memory and then parse it with json.load , but you don't want to do that).

Also, please don't post screenshots! They're harder to make sense of, and require clicking.

I would like to add that you are not reading the file in the code, which leads to the following (example):

with open('test.txt', 'r') as file:
    print(file)

Output:

<_io.TextIOWrapper name='test.txt' mode='r' encoding='UTF-8'>

Use read():

with open('test.txt', 'r') as file:
    print(file.read())

Output:

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.