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.
Simply make a custom function and call it for every time you want to add a new line to your text file. For example:
def f(dump):
file = open('myfile.txt', 'a')
file.write(dump)
file.write('\n')
file.close()
and then pass it the values you want to write on the fly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.