Ad

How Can I Append A Boolean Value To A Text File?

I'm trying to add a boolean value to a text file but get this error:

Traceback (most recent call last):
  File "/Users/valentinwestermann/Documents/La dieta mediterranea_dhooks.py", line 32, in <module>
    f.write(variant["available"])
TypeError: write() argument must be str, not bool 

Does anyone have an idea how to fix this? : ) It is supposed to work as a restock monitor and make a text version of the product availability when the bot launches and then constantly compare it and notifiers the user about product restocks.

import bs4 as bs
import urllib.request
import discord
from discord.ext import commands
from dhooks import Webhook
import requests
import json

r = requests.get("https://www.feature.com/products.json")
products = json.loads((r.text))["products"]

for product in products:
    print("============================================")
    print(product["title"])
    print(product["tags"])
    print(product["published_at"])
    print(product["created_at"])
    print(product["product_type"])
    for variant in product["variants"]:
        print(variant['title'])
        print(variant['available'],"\n")

        data =("available")
with open("stock_index.txt","w") as f:
    for product in products:        
        for variant in product["variants"]:
            if variant['available'] == True:
                f.write(product["title"])
                f.write(variant['title'])
                print(variant["available"])
                f.write("--------------------")
Ad

Answer

You can convert to a string first:

f.write(str(variant["available"]))
Ad
source: stackoverflow.com
Ad