比较新旧值 python
Compare old and new values python
当我比较两个 url 到 if else
时,我得到的总是其他(打印 test2
),如果两个值相同则无关紧要。为什么?代码是:
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()
在 compare.txt
中是 - https://example.com/
在 url 变量中 - https://example.com
这个:
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
将不起作用。文件是基于流的,一旦流到达文件末尾,您就可以使用它们。
使用
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)
输出(第一次使用 tata 后,然后 curr_url
纹身):
Read: tata
Wrote: tattoo
当我比较两个 url 到 if else
时,我得到的总是其他(打印 test2
),如果两个值相同则无关紧要。为什么?代码是:
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()
在 compare.txt
中是 - https://example.com/
在 url 变量中 - https://example.com
这个:
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
将不起作用。文件是基于流的,一旦流到达文件末尾,您就可以使用它们。
使用
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)
输出(第一次使用 tata 后,然后 curr_url
纹身):
Read: tata
Wrote: tattoo