Как сравнить два текстовых файла с python и, если определенная строка отличается, вызвать вывод? - PullRequest
0 голосов
/ 04 мая 2020

Мне было интересно, можно ли сравнить два текстовых файла (индексный файл, созданный при запуске программы, и один, создаваемый каждые 15 секунд) и вызвать событие, если доступная строка отличается для продукта?

Я с нетерпением жду ваших ответов:)

import bs4 as bs
import urllib.request
import discord
from discord.ext import commands
from dhooks import Webhook
import requests
import json
import time
from datetime import datetime
linex = 1
liney = 1

r = requests.get("https://www.feature.com/products.json")
products = json.loads((r.text))["products"]
now = datetime.now()
data = ("available")
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:
    f.write(str(now)+"\n")
    f.write("============================================\n")
    f.write("=============STOCK-AVAILABILITY============\n")
    f.write("============================================\n\n\n")
    for product in products:        
        for variant in product["variants"]:
            if variant['available'] == True:
                f.write(product["title"]+"\n")
                f.write(variant['title']+"\n")
                f.write((str(variant["available"])+("\n")))
                f.write("--------------------\n")

file1 = open('stock_index.txt', 'r')
file2 = open('stock_index_hourly-update.txt', 'r')
FO = open('some_output_file.txt', 'w')

for linex in file1:
    for liney in file2:
        if linex == liney:
            FO.write("%s\n" %(linex))
            linex +=1
            liney +=1

FO.close()
file1.close()
file2.close()
...