I am trying to make a loop that writes in a text file the date each time en event happens. but i can't get it to work since i need an infinite loop to run the program. If i put myfile.close() inside the loop even inside the "if x[14]=="track":" i get:
myfile.write(wri)
ValueError: I/O operation on closed file.
However if i place it outside the loop the file Doesn't close and nothing is written in the output file.
Here is the code
while 1 :
print("yes")
response = requests.get('https://api.spotify.com/v1/me/player/currently-playing', headers=headers)
soup2 = BeautifulSoup(response.text, "html.parser")
x=re.findall('"([^"]*)"', str(soup2))
if isinstance(x, list)==True:
if len(x)>=15:
print(x[14])
if x[14]=="track":
os.system("TASKKILL /IM spotify.exe")
sleep(2)
subprocess.Popen("C:/Users/nebbu/AppData/Roaming/Spotify/Spotify.exe")
sleep(2)
import pyautogui
pyautogui.press("playpause")
pyautogui.press("l")
print(x)
wri=str(date)+"- -"+str(x[13]+": "+str(x[14]))
myfile.write(wri)
myfile.close()
The loop never ends, i don't know if it has to end to close the file or if there is another way of doing it.