2

I need to use stream redirectiton in Popen call in python to use bat file with wine. I need make this:

wine32 cmd  < file.bat

It works when I run it manually from terminal, however when I try to call it from python:

proc = Popen('wine32 cmd < file.bat',stdout = PIPE)

I got error: No such file or directory

How to manage with that?

Thanks

jmmk
  • 93
  • 1
  • 9

2 Answers2

3

Try this:

import sys

#...

with open('file.bat', 'r') as infile:
    subprocess.Popen(['wine32', 'cmd'], 
        stdin=infile, stdout=sys.stdout, stderr=sys.stderr)

Make sure that each argument to wine32 is a separate list element.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • "With the default settings of None, no redirection will occur; the child’s file handles will be inherited from the parent", thus no need to have stdout, stderr there. – Antti Haapala -- Слава Україні Jan 26 '15 at 11:07
  • 1
    @AnttiHaapala: Ah, ok. As you might guess I haven't played with subprocess much. :) Which reminds me I really should update my old scripts that use os.popen & friends... – PM 2Ring Jan 26 '15 at 11:14
  • @jmmk: `stdout=sys.stdout, stderr=sys.stderr` are probably unnecessary -- test it. – jfs Jan 27 '15 at 07:02
0

maybe you can check this thread.. https://stackoverflow.com/a/5469427/3445802

from subprocess import Popen
p = Popen("batch.bat", cwd=r"C:\Path\to\batchfolder")
stdout, stderr = p.communicate()
Community
  • 1
  • 1
agaust
  • 97
  • 3
  • 19
  • But I call it from linux through wine. Popen("file.bat" ... won't work here – jmmk Jan 24 '15 at 12:10
  • are you call using idle? – agaust Jan 24 '15 at 12:13
  • What do you mean? I run python script simply via gnome-terminal – jmmk Jan 24 '15 at 12:16
  • why not use idle python in ubuntu? there .bat file can be executed with idle (because idle is the default for Windows). I used to use it if I want to run the .bat file ... if the gnome terminal would be in error, because it obviously does not support the .bat file in gnome terminal. – agaust Jan 24 '15 at 12:31
  • Not ubuntu. bat may be executed using stream redirecting as I wrote. I need to get '<' stream redirect working – jmmk Jan 24 '15 at 12:38
  • maybe for `Popen` module, you can try this? `Popen(r"wine32 cmd < file.bat",stdout = PIPE)` – agaust Jan 24 '15 at 12:47
  • It doesn't work. I tried also `Popen(['wine32', 'cmd', '<', 'file.bat'], stdout = PIPE)` Then I get no errors however the code isn't run – jmmk Jan 24 '15 at 12:53
  • whether to use `Popen` as you wrote has been able to call `cmd`? – agaust Jan 24 '15 at 12:56
  • `>>> import subprocess` `>>> with open('sample.bat', 'r') as filen: subprocess.call('wine application.exe & cmd', shell=True)` – agaust Jan 24 '15 at 13:39