0

I'm creating a program in Python and I have a problem with a for loop and opening program in this loop. The program is supposed to run e.g. 5 times and it only runs once

import subprocess

z = int(input())

def run():
    subprocess.run('notepad.exe')

a = 0
while(a<z):
    a = a + 1
    run()

I've tried creating a function and replacing the for loop with a while loop but it doesn't work. Sorry for my English

double-beep
  • 5,031
  • 17
  • 33
  • 41
Rocket
  • 1
  • 4

3 Answers3

0

Now it depends what you want. subprocess.run() opens the program, and pauses the code until you close notepad. Then, it will re open it, and do it z many times.

If you want to open z many instances of notepad at the same time you need to use `subprocess.Popen('notepad.exe', '-new-tab')

From the look of the code this may or may not be an issue. Python doesn't know where notepad.exe is located. You need to add the full path of the executable file. For example, try this:

import subprocess

z = int(input())

def run():
    subprocess.run(r'C:\WINDOWS\system32\notepad.exe') #For most people this is the location of notepad.exe

a = 0
while(a<z):
    a = a + 1
    run()
The Pilot Dude
  • 2,091
  • 2
  • 6
  • 24
0

Use subprocess.Popen('notepad.exe'), this will put the process in the background.

subprocess.run() will wait for the exit code I think, which waits until you close the notepad.

So full code is

import subprocess

z = int(input())

def run():
    subprocess.Popen('notepad.exe')

a = 0
while(a<z):
    a = a + 1
    run()
0

You need to do pip install AppOpener first (or however you install modules for your device).

from AppOpener import open

z = int(input('Enter the number of times to open notepad: '))

def run():
    open('notepad')
    

a = 0
while(a < z):
    a = a + 1
    run()

Additionally, for running any different apps, just change 'notepad' to the name of the app. (For Example: 'facebook')

Jonathan
  • 185
  • 9