When I am comparing two urls through if else
i am getting always else (Prints test2
), doesnt matter if two values are same. Why? Code is:
def derpypost():
threading.Timer(10.0, derpypost).start()
print(Fore.CYAN + Style.BRIGHT + "Прошло " + times +
" минут, начинаю постить картинку в " + vk_group_name)
print(" Получаю URL картинки")
real_dp_tags = dp_tags.replace("'", "").replace("'", '')
for image in Search().query(real_dp_tags).limit(1):
url = image.image
source = image.url
post_tags = image.tags
sourceurl = image.source_url
old_url = open('compare.txt', 'r')
print(old_url.read())
olded_url = old_url.read()
if olded_url == url:
print("test")
else:
print('test2')
old_url.close()
with open('compare.txt', 'w') as f:
f.write(url)
print('WRITTEN!' + url)
f.close()
derpypost()
In compare.txt
is - https://example.com/
In url variable - https://example.com
This:
old_url = open('compare.txt', 'r') # stream at pos 0 print(old_url.read()) # stream at end of file olded_url = old_url.read() # nothing to put into variable
will not work. Files are stream based, as soon as you consume them once the stream is at the end of the file.
Use
def writeFile(urlToWrite):
fn = 'compare.txt'
with open(fn, 'w') as createFile:
createFile.write(urlToWrite)
print("Wrote: " + urlToWrite) # if you want it to see on console.
def readFile():
fn = 'compare.txt'
old_url = ""
try:
with open(fn, 'r') as rf:
old_url = rf.read()
print("Read: " + old_url) # if you want it to see on console.
except FileNotFoundError: # create file if not exists
writeFile("")
return old_url
curr_url = "tata"
old = readFile()
if old != curr_url:
writeFile(curr_url)
Output (after 1st using tata, then tattoo for curr_url
):
Read: tata
Wrote: tattoo