0

In my Python script, I create a .bat file (many actually), and I run them via

os.startfile(blah)

Everything works like expected, however, those terminals die after finishing. I want to keep them open, so that I can type more commands manually in those opened terminals.

How?

Alex Deft
  • 2,531
  • 1
  • 19
  • 34
  • Does [this solution](https://stackoverflow.com/a/54861143/1586231) work for you? It seems like `os` holds on to the working directory, if that's the important variable for your purposes ... or do you need something else, e.g., to keep track of specific bash variables or something? – Max von Hippel Aug 19 '19 at 20:54
  • 1
    @MaxvonHippel Im trying to create an autograder, students are submitting code, I want to test each one on 12 cases. I want the terminals to stay open when done. Directory is the same for all. – Alex Deft Aug 19 '19 at 21:02

1 Answers1

0

You could try using the cmd command to run the batch file, and then use command line arguments for cmd to modify its behavior. For example:

os.startfile("cmd.exe /k blah.bat")

Documentation of the available command line arguments can be found here:

https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/cmd

  • I get this error: FileNotFoundError: [WinError 2] The system cannot find the file specified: 'cmd.exe \\k .\\batches\\test11.bat' – Alex Deft Aug 19 '19 at 20:58
  • try `os.system("cmd.exe /k .\\batches\\test11.bat")`. Also be careful, before `k` comes a forward slash `/` not a back slash \\. – Stam Kaly Aug 19 '19 at 21:03
  • @StamKaly Everything freezes when I switch to os.system – Alex Deft Aug 19 '19 at 22:04
  • `os.startfile` doesn't support command-line arguments. (In principle it could be extended to support a command line; it's just calling `ShellExecuteW`.) `os.system` runs a command via `cmd /c`, so it waits unless the command uses `start`, e.g. `os.system('start blah.bat')`. This implicitly runs the batch script via `cmd /k blah.bat` and uses the `CreateProcessW` creation flag `CREATE_NEW_CONSOLE` to request a new console window. – Eryk Sun Aug 20 '19 at 00:22