Smack Alpha
Smack Alpha

Reputation: 1980

Check content of text file and add content if it's not there

I am new to python. I am designing a quotes app using python. I am getting the quotes of the day from the brainy quote websites using BeautifulSoup. I would append it to the text file. In here, if the quotes of the day are already added, when I execute the program again, it should skip it. How to make it possible

Here's the code:

from bs4 import BeautifulSoup
import socket
import requests
import subprocess
import datetime
def quotenotify():
    timestamp = datetime.datetime.now().strftime("%b %d")
    res = requests.get('https://www.brainyquote.com/quote_of_the_day')
    soup = BeautifulSoup(res.text, 'lxml')

    image_quote = soup.find('img', {'class': 'p-qotd bqPhotoDefault bqPhotoDefaultFw img-responsive delayedPhotoLoad'})
    quoteday=image_quote['alt']
    text_file = open("quotes.log", "a+")
    text_file.write("%s"%timestamp+"\t"+"%s"% quoteday)
    text_file.write("\n")
    text_file.close()
    return
quotenotify()

output in a file:

Mar 29  Where there is a great love, there are always wishes. - Willa Cather
Mar 29  Where there is great love, there are always wishes. - Willa Cather

Upvotes: 1

Views: 129

Answers (2)

DirtyBit
DirtyBit

Reputation: 16772

Continuing from the comments:

from bs4 import BeautifulSoup
import requests
import datetime

def quotenotify():
    timestamp = datetime.datetime.now().strftime("%b %d")
    res = requests.get('https://www.brainyquote.com/quote_of_the_day')
    soup = BeautifulSoup(res.text, 'lxml')
    image_quote = soup.find('img', {'class': 'p-qotd bqPhotoDefault bqPhotoDefaultFw img-responsive delayedPhotoLoad'})['alt']
    with open("quotes.log", "w+") as f:
        if image_quote not in f.read():
            f.write("%s"%timestamp+"\t"+"%s"% image_quote + "\n")

quotenotify()

EDIT:

Since using the mode w+ would truncate the file, I'd suggest going with pathlib:

from bs4 import BeautifulSoup
import requests
import datetime
from pathlib import Path

def quotenotify():
    timestamp = datetime.datetime.now().strftime("%b %d")
    res = requests.get('https://www.brainyquote.com/quote_of_the_day')
    soup = BeautifulSoup(res.text, 'lxml')
    image_quote = timestamp + "\t" + soup.find('img', {'class': 'p-qotd bqPhotoDefault bqPhotoDefaultFw img-responsive delayedPhotoLoad'})['alt']
    with open("quotes3.log", "a+") as f:
        contents = [Path("quotes3.log").read_text()]
        print(contents)
        print(image_quote)
        if image_quote not in contents:
            f.write("%s" % timestamp + "\t" + "%s" % image_quote + "\n")

quotenotify()

Upvotes: 1

As mentioned by @DirtyBit, you should open the file in read mode first and load the content onto a variable.

You can see in my example below that I load the content onto a variable, then append to the file only if the variable is not inside the text file.

text_file = open('test-file.txt', 'r+')
read_the_file = text_file.read()
text_file.close()

text_file = open('test-file.txt', 'a+')
new_string = 'Smack Alpha learns python'

if new_string not in read_the_file:
    text_file.write(new_string + '\n')
text_file.close()

Upvotes: 0

Related Questions