0

According to this answer I can execute the .bat file in Python. Is it possible not only execute .bat file, but also send a string as a parameter, which will be used in Java programm?

What I have now:

Python script:

import subprocess

filepath="C:/path/to/batch/myBatch.bat"
p = subprocess.Popen(filepath, shell=True, stdout = subprocess.PIPE)

stdout, stderr = p.communicate()
print p.returncode # is 0 if success

Java programm:

public static void main(String[] args) {
    System.out.println("Hello world");
}

What I want to have:

Python script:

import subprocess

parameter = "C:\\path\\to\\some\\file.txt"
filepath="C:/path/to/batch/myBatch.bat"
p = subprocess.Popen(filepath, shell=True, stdout = subprocess.PIPE, parameter)

stdout, stderr = p.communicate()
print p.returncode # is 0 if success

Java programm:

public static void main(String[] args) {
    System.out.println("Hello world");
    System.out.println(args[1]); // prints 'C:\\path\\to\\some\\file.txt'
}

So the main idea is to send a string from python as parameter to a java programm and use it. What I have tried is following:

import os
import subprocess

filepath = "C:\\Path\\to\\file.bat"
p = subprocess.Popen(filepath, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
grep_stdout = p.communicate(input=b"os.path.abspath('.\\file.txt')")[0]
print(grep_stdout.decode())
print(p.returncode)
print(os.path.abspath(".\\file.txt"))

Output:

1
C:\\path\\to\\file.txt

1 means, that something went wrong. And it is so, because Java programm looks like this:

public static void main(String[] args) throws IOException {
    String s = args[1];
    // write 's' to a file, to see the result
    FileOutputStream outputStream = new FileOutputStream("C:\\path\\to\\output.txt");
    byte[] strToBytes = s.getBytes();
    outputStream.write(strToBytes);
    outputStream.close();
}

and after execution file.bat in Python, output.txt is empty. What am I doing wrong?

4
  • It's a RTFM-kind of answer ... you must call Popen with a list containing the "executable" and every single parameter something likep = subprocess.Popen([filepath,os.path.abspath('.\\file.txt')], stdout=subprocess.PIPE, stdin=subprocess.PIPE). You can use communicate if the called "program" reads from stdin. Commented Jul 31, 2018 at 15:04
  • Not understanding why you need a batch-file? Commented Jul 31, 2018 at 15:14
  • @Squashman I am becoming this file when call mvn clean package command Commented Jul 31, 2018 at 19:07
  • @LohmarASHAR thank you for feedback! I will try your suggestion and tell you if it work Commented Jul 31, 2018 at 19:08

1 Answer 1

2

The problem in your code is that you are calling subprocess.Popen in the wrong way. In order to achieve what you want, as the documentation states, Popen should be called with a list of strings containing the "executable" and all the other arguments separately. More precisely, in your case it should be:

p = subprocess.Popen([filepath, os.path.abspath('.\\file.txt')], stdout=subprocess.PIPE, stdin=subprocess.PIPE)

As a side note, you should/would use .communicate(...) only when the "executable" launched "asks" for input (stdin).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.