-1

Alright so well,basically im trying to run a random bat file from a folder without opening a new command prompt window by using python(i open the python file and then the bat file that is choosen by the python file will run in the same cmd window) ,as soon as the bat file runs all the code it had,it will run the python file again in that same cmd window,I've tried using this https://stackoverflow.com/a/54110207/18990761 but it opens a new cmd window instead of using the same one so yeah,Sorry for my grammar english is not my first language.

Flawed
  • 11
  • 2
  • Python is a much more powerful script interpreter than the Windows Command Processor `cmd.exe`. There is (nearly) never the need to run a batch file from within a Python script. But if you want to do that nevertheless, use [subprocess.Popen](https://docs.python.org/3/library/subprocess.html) with `%SystemRoot%\System32\cmd.exe` as executable to run with the arguments `/D` and `/C` and the fully qualified batch file name enclosed in `"` as arguments and other options you prefer for this task. Please note that `%SystemRoot%` references here the predefined environment variable `SystemRoot`. – Mofi Apr 29 '22 at 17:01
  • There can be used [os.environ\["SystemRoot"\]](https://docs.python.org/3/library/subprocess.html) to get the directory path to the Windows directory and use `"C:\\Windows"` if there is returned extremely unlikely no string because of a user deleted with `set SystemRoot=` the predefined environment variable before running `python.exe` in a command prompt window. The string of Windows directory is concatenated with `"\\System32\\cmd.exe"` to get the fully qualified file name of the Windows Command Processor. – Mofi Apr 29 '22 at 17:06
  • I strongly recommend to read also the Microsoft documentations about the Windows kernel library function [CreateProcess](https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) and the [STARTUPINFO](https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow) structure. The `subprocess` module is on Windows a Python wrapper for this function used with this structure by any Windows executable capable running another executable without or with opening a window as defined on calling `CreateProcess`. – Mofi Apr 29 '22 at 17:08

1 Answers1

0

If you don't want your program to open a command window, Import all your commands to the python and run it from python.

Example:

# importing os module  

import os  

  
# Command to execute 
# Using Windows OS command 

cmd = 'notepad'

  
# Using os.system() method 
os.system(cmd) 
High-Octane
  • 1,104
  • 5
  • 19
  • i want it to randomly select a set of commands too though – Flawed Apr 29 '22 at 15:39
  • @Flawed you didn't mention this in your question. Add your commands to a list and use random.choices method to get N number of random commands. – High-Octane Apr 29 '22 at 16:50
  • i did,thats why i said "Is there a way to run a random .bat file from a folder using python" there are many bat files with different code in the folder and i want the python file to select one of them randomly and run it – Flawed Apr 30 '22 at 05:05