I wrote a program that calls another program from itself through the subprocess library and then terminates itself through sys.exit().
But it's not that simple. There should be a list of questions. (note, it will not be about the script itself, but about the application of this script created through pyinstaller)
- When I go to the task manager, in the details tab, I see there 4 test.exe, 2 of them are running in the current folder, the other 2 are in
APPDATA\Local\test os.remove(o)is not executedpath + f'\\{k}.txt'saves only inAPPDATA\Local\test, andf'{k}test.txt'only to current folder
Apparently, the program does not start from appdata at all, but it is not so, because in the task manager it is shown, even twice. What is the reason for this behavior? And how to fix it?
UPD: I have ensured that files are saved only in upadte, writing os.chdir(path) after else:. But first execution still cannot complete.
import sys
import os
import time
path = os.path.dirname(os.getenv('APPDATA')) + '\\Local\\test'
try:
os.mkdir(path)
except OSError:
pass
if not os.path.isfile(path + '\\test.exe'):
with open(path + '\\info.txt', 'w', encoding='utf-8') as f:
f.write(sys.argv[0])
subprocess.call(['copy', sys.argv[0], path + '\\test.exe'], shell=True)
subprocess.call(path + '\\test.exe', shell=True)
sys.exit()
else:
with open(path + '\\info.txt', 'r', encoding='utf-8') as f:
o = f.readline()
if os.path.isfile(o):
try:
os.remove(o)
except:
pass
k = 0
while True:
time.sleep(5)
with open(path + f'\\{k}.txt', 'w', encoding='utf-8') as f:
f.write('test message 1')
with open(f'{k}test.txt', 'w', encoding='utf-8') as f:
f.write('test message 2')
k += 1