I need to write a programm that opens a windows explorer on the folder that the current script is on, every time user inputs 's'. And every time user inputs 'e' i need to close active windows exlplorer process.
this is my code:
import msvcrt
import os
import subprocess
input_char = ""
ALL_PROCESSES = []
file_directory = os.getcwd()
while(input_char != b'~'):
input_char = msvcrt.getch()
match input_char:
case b's':
#OPEN EXPLORER
proc = subprocess.Popen(f'explorer "{file_directory}"')
ALL_PROCESSES.append(proc)
continue
case b'e':
#CLOSE ACTIVE PROCESS
subprocess.call(['taskkill', '/F', '/T', '/PID', str(ALL_PROCESSES[-1].pid)])
ALL_PROCESSES.pop()
continue
else:
print("Finished")
#close explorer
Every time user enters 's' i open internet exloprer and save subprocess object to array. And everytime user enters 'e' i try to taskkill last opened process by its pid and i get next error:
ERROR: The process "7140" not found.
Why does the saved in array process changes its pid over time, and how can i keep track of it so i can successfully close it?