How to close multiple windows of the same program through the same program and leaving only one program active

Question:

I use the right click menu to launch a program that moves files to a folder and do some work on them. The problem is that when i do this with multiple files, it starts multiple instances of the program. If i have 50 files, it will launch the app 50 times.

I need to have only one window not multiple windows. So far i manage to make it work sometimes with this code, but what i need is to make it work a 100% of time:

# ========================================================
#this only catches the file adress that was right clicked and launch with the app. Then it moves it to the installer folder
try:
       
 # THIS CATCHES THE SELECTED FILE LINK TO BE MOVED TO THE  INSTALL FOLDER IF IT IS SUCCESSFULL

    variable= sys.argv[1]

    index = -1

    for i in variable:
        index = index + 1
        if "\" in i:
           sum= index

       # MOVE TO INSTALLER DIRECTORY 
    shutil.move(variable, Install_folder + f"Installerfiles{str(variable[sum+ 1::])}")

except:
      print('FILE NOT ADDED THROUGH THE MOVE TOOL')

#=========================================================

processlist = list()

time.sleep(int(time.process_time())*random.randint(1,3))

for process in psutil.process_iter():
    processlist.append(process.name())

if processlist.count("program.exe") >= 4:
    sys.exit()

My guess is that the programs start activating at the same time and that could be why they are closing instead of letting only one active window remain. I only have 2 months of python so hope you could help me. Thank you in advance for reading.

My other alternate solution to this is separate the programs into two. One for moving the files and another for doing the work with the files. But this solution it is not the desired one.

Solution:

try:

    variable = sys.argv[1]
    index = -1

    # THIS CATCHES THE SELECTED FILE LINK TO BE MOVED TO THE HANDY INSTALL FOLDER IF IT IS SUCCESSFULL
    for i in chetita:
        index = index + 1
        if "\" in i:
            sum = index

    # CHOOSES DIRECTORY WHERE THE FILES TO BE INSTALLED ARE
    shutil.move(variable, f"{Install_folder}\program\InstallFolder{str(variable[sum + 1::])}")
    os.makedirs(f"{Install_folder}\program\Queue")
    tiger = []
    serpent = []
    while True:
        time.sleep(1)
        serpent  = os.listdir(f"{Install_folder}\program\InstallFolder")
        time.sleep(1)
        tiger = os.listdir(f"{Install_folder}\program\InstallFolder")
        if tiger == serpent:
            break
except:
    if os.path.exists(f"{Install_folder}\program\Queue"):
       sys.exit()
    print('application executed directly through the .exe').

Basically when all the windows open, the first window on compliting the task of moving a file. Creates a file called Queue and enters a while loop that is active until the content of the folder match 2 variables(meaning all the other windows finish their work and are closed). The other programs closes because they get an execption when they tried to create that folder. When all the other programs are closed, the waiting windows will leave the while loop and start working on the files.

Asked By: shadowuy

||

Answers:

Author your python app so it behaves in this way.

from pathlib import Path

QUEUE_FILE = Path("~/queue.txt").expanduser()

...

if __name__ == "__main__":
    if QUEUE_FILE.exists():
        ...   # do the old app behavior in an interactive window
    else:
        append_filename(QUEUE_FILE, sys.argv)

So there are two modes of operation.

That second if clause will very quickly service any right-click requests.
It does almost no work, merely writing a line of text to a central queue file.

The first if clause mostly behaves the same as your current app,
and it keeps a single window open while you’re interactively working with it.
The difference is that, instead of accepting filename(s) in sys.argv,
it accepts most of those fifty filenames via the central queue file.

Upon exiting, it must delete the queue file.
That sets us up for a subsequent interaction.

Answered By: J_H
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.