I am trying to write the title of a opened youtube video on "api obs.txt" and its URL on "link obs.txt". The title gets written but it says "ValueError: I/O operation on closed file." when writing the URL.
while True:
atual = driver.current_url
with open('link obs.txt', 'r') as file:
linksalvo = file.readline()
#if current is different than the saved url
if atual != linksalvo:
#write the new title on the file for the api to read
with open('api obs.txt', 'w') as f:
sys.stdout = f
html = urlopen(atual)
html = BeautifulSoup(html.read().decode('utf-8'), 'html.parser')
print(html.title.get_text())
#write the URL of the current video to the file for comparison later
with open('link obs.txt', 'w') as f:
print(driver.current_url) #"ValueError: I/O operation on closed file." Happens on this line
sys.stdout = original_stdout
- Unrelated problem: the loop is a while true loop but if the page doesnt have a title it will give a error message and halt. I also dont know how to make the code ignore the error and keep looking until the current page has a title.