-1

I want to start multiple applications using python script, basically there will be one text file which contains the paths of executable files and I want to open all of those applications one by one. I tried it by using subprocess.Popen but it opens only first application from text file and even it executes only when text file has path of only one application, if I try to save multiple paths in text file then python script shows an error: [WinError 2] The system cannot find the file specified I tried with readlines method too, but it didn't worked

Below is the code I'm trying to execute

with open(path) as file:
   for x in file:
      subprocess.Popen(x)


Paths.txt file: 
C:\Windows\notepad.exe 
C:\Program Files\Sublime Text\sublime_text.exe
Rushi
  • 23
  • 7
  • I think it should be `subprocess.Popen([x])` it you don't want argument parsing – Thomas Weller Feb 06 '22 at 21:22
  • @ThomasWeller its still giving same `error: hp, ht, pid, tid = _winapi.CreateProcess(executable, args, FileNotFoundError: [WinError 2] The system cannot find the file specified` – Rushi Feb 06 '22 at 21:28
  • 1
    What is the output of `print(repr(x))`? – mkrieger1 Feb 06 '22 at 21:29
  • @mkrieger1 output is: ` 'notepad.exe\n' 'cmd.exe' ` – Rushi Feb 06 '22 at 21:32
  • if I keep only notepad.exe in path.txt file then it executes normally but when I add another application (cmd.exe) path on second line then it gives `FileNotFound` error – Rushi Feb 06 '22 at 21:37
  • If I add two lines then code does not work, but if I give path of any single executable file then code works. It just dont work with multiple paths – Rushi Feb 06 '22 at 21:43
  • 2
    And if you use `subprocess.Popen(x.strip())`? – mkrieger1 Feb 06 '22 at 21:48

2 Answers2

0

When you pass a simple string to Popen, it tries to parse it as a command line. Thus, it's going to try to execute the program C:\Program and pass as parameters Files/Sublime, then Text\sublime_text.exe. You can see how that would be suboptimal

To get around this parsing, you need to pass the arguments as a list:

with open(path) as file:
   for x in file:
      subprocess.Popen([x])
Tim Roberts
  • 34,376
  • 3
  • 17
  • 24
0

The following works for me.

import subprocess
import time

with open('path.txt') as file:
   for x in file:
      subprocess.Popen(x.strip())

time.sleep(10)

# Content of path.txt:
#
# C:\Windows\notepad.exe 
# C:\Program Files (x86)\Notepad++\notepad++.exe
# C:\Program Files (x86)\Simple Sudoku\simplesudoku.exe
Alejandro QA
  • 171
  • 1
  • 7