After you do sys.stdout = f
, any operation on f
will also be done on sys.stdout
. Leaving that with
block closes f
, so it also closes sys.stdout
.
Then when you later do print(driver.current_url)
, it tries to write this to sys.stdout
, but it's closed, so you get an error. You need to use sys.stdout = f
again after opening the second file.
I recommend you don't redefine sys.stdout
in the first place. You can use the file
argument to print()
to write to a specific file object.
if atual != linksalvo:
#write the new title on the file for the api to read
with open('api obs.txt', 'w') as f:
html = urlopen(atual)
html = BeautifulSoup(html.read().decode('utf-8'), 'html.parser')
print(html.title.get_text(), file=f)
#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, file=f) #"ValueError: I/O operation on closed file." Happens on this line
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…