2

I have the following problem: from a Python script I need to run a CMD where I use 3 parameters, like below:

path_to/my.exe path/file.abc > path/file.txt

The closest solution found till now is something like:

os.system("start cmd /k D:/.../myexecutable.exe D:/.../file1.dat > D:/.../file1.txt")

But this only creates a blank file called file1.txt, and in console I have the output produced by my EXE visible, but that content is not redirected to text file as I expect.

Also it will be nice to hide the console during the execution and wait for finish.

This part shall be applied to multiple files (this I manage to do).


Applying info from comments now the text is in the file, using this code:

os.system("cmd /k D:\\...\\myexecutable.exe D:\\...\\file1.dat ^> D:\\...\\file1.txt")

Left with one problem: in the command window, at end of file processing it's printed the path to my Python file. How can I close it?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Adrian
  • 21
  • 3
  • What is the purpose of `start` in your code? Note that the Windows path separator is `\ `but not `/`… – aschipfl Jun 05 '20 at 13:05
  • I've used start simply because I took it from an example .... – Adrian Jun 05 '20 at 13:07
  • 2
    Are you getting the line to run from a third-party? Because it would be simpler to use `subprocess` to run your binary, and redirect the subprocess's stdout to whatever file you want. Alternatively, you can shove the entire thing to run in `subprocess` using `shell=True`, that's less safe and reliable but it should also work fine. – Masklinn Jun 05 '20 at 13:10
  • Following @aschipfl suggestion, I've removed the start from the string and replaced all / with \\. Now something is better, a blank console is opened, but after waiting a while I close it manually. I've checked the txt file and now contains all the info I need. So, how can i force the console to close when the execution is finished? – Adrian Jun 05 '20 at 13:14
  • you need to escape the `>` character to stop it being consumed by `cmd`. The usual way to do that is to use the caret escape character, `^`. – Compo Jun 05 '20 at 13:18
  • @Masklinn: shame on me but I'm completely new to this level of programming. Resuming, I have one exe file, let's call it a.exe and a b.dat file. I know to use it in command, like that:
    a.exe b.dat > b.txt
    how can I translate this to a subprocess call?
    – Adrian Jun 05 '20 at 13:21
  • @Compo: as I said a bit ago, now I manage to have the info written in my txt file but i don't know how to make the cmd window to close when the execution is completed. – Adrian Jun 05 '20 at 13:24
  • `with open('b.dat', 'wb') as out: subprocess.run(['a.exe'], stdout=out)` should do the trick. You may want to add e.g. `check=True` in case you want an exception to be raise should the command signal a failure. – Masklinn Jun 05 '20 at 13:24
  • 4
    Use `cmd`'s `/c` option, instead of its `/k` option! – Compo Jun 05 '20 at 13:26
  • 1
    @Adrian the entire point of the `/K` flag is to run the command *then keep the shell alive*, so maybe don't do that, and use `/C` instead (runs the command then shuts down)? – Masklinn Jun 05 '20 at 13:27
  • Look at this Post: https://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python?rq=1 maby it solves you Propleme – RegenJacob Jun 05 '20 at 13:29
  • Using @Compo's tips I manage to make it work, also I will try later the solution form Masklinn. Thank you guys! – Adrian Jun 05 '20 at 13:33
  • 1
    I [formatted](https://stackoverflow.com/help/formatting) your post twice now to show you how it could/should look like; next time when you edit it, please format it correctly on your own! – aschipfl Jun 05 '20 at 13:34
  • 1
    Instead of ecaping the redirection character, `os.system("cmd /c D:/.../myexecutable.exe D:/.../file1.dat ^> D:/.../file1.txt")`, you could alternatively try quoting your, `/c`, option: `os.system("cmd /c \"D:/.../myexecutable.exe D:/.../file1.dat > D:/.../file1.txt\"")` – Compo Jun 05 '20 at 13:50

0 Answers0