0

I am trying to run the following command in Python after I have cd in C:\Program Files\Java\jdk-11.0.10\bin\javaw.exe

"@C:\Users\danie\IdeaProjects\EV_example\EV_example\matsim-libs\contribs\ev.temp-ChargerLocation-args-1617369801473.txt" When being in cmd I can simply move to C:\Program Files\Java\jdk-11.0.10\bin\javaw.exe and then execute the "@C:\Users\danie\IdeaProjects\EV_example\EV_example\matsim-libs\contribs\ev\.temp-ChargerLocation-args-1617369801473.txt". It should include the quotation marks in order to correctly run.

however, when I am using the subprocess.run(["cd C:\Program Files\Java\jdk-11.0.10\bin\javaw.exe", ""@C:\Users\danie\IdeaProjects\EV_example\EV_example\matsim-libs\contribs\ev\.temp-ChargerLocation-args-1617369801473.txt""]) the command seems to gets confused with the double quotation marks, (see photo attached).

enter image description here

Is there any way to be able to run that just like being in my cmd but to be called from Python?

talex
  • 17,973
  • 3
  • 29
  • 66
user3474218
  • 51
  • 1
  • 7
  • You can escape a character in Python code using \ – Markiian Benovskyi Apr 02 '21 at 13:40
  • Please [don’t post images of code or error messages;](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557) tiny low-contrast images add insult to injury, and should already by themselves convince you why ttis is a bad idea. – tripleee Apr 02 '21 at 13:49
  • To be honest, I would use `os.system` to run commands in a shell. – coderman1234 Apr 02 '21 at 13:58
  • @coderman1234, I managed running it with os.system like this: `os.system(r'"java.exe @C:\Users\danie\IdeaProjects\EV_example\EV_example\matsim-libs\contribs\ev\.temp-ChargerLocation-args-1617369801473.txt"')` However, although this is working perfect I am trying to do exactly the same think for another java class : `os.system(r'"java.exe @C:\Users\danie\IdeaProjects\EV_example\EV_example\matsim-libs\contribs\ev\.temp-MyRunEvExample-args-1617459572859.txt"')` but for this one I get the error: The filename, directory name, or volume label syntax is incorrect. The location and everything same. – user3474218 Apr 03 '21 at 14:46

2 Answers2

1

There should be no reason per se to cd to a specific directory to run a command whose binary is in that directory (see also https://stackoverflow.com/a/66860904/874188) but if you really need it, use the cwd keyword argument to subprocess.run().

subprocess.run([r"C:\Program Files\Java\jdk-11.0.10\bin\javaw.exe",
        r"@C:\Users\danie\IdeaProjects\EV_example\EV_example\matsim-libs\contribs\ev\.temp-ChargerLocation-args-1617369801473.txt"],
    # probably completely unnecessary
    cwd=r"C:\Program Files\Java\jdk-11.0.10\bin",
    # probably also a good idea
    check=True)

In the absence of shell=True, no explicit quoting should be necessary or useful in the command string (though notice the raw string in order to correctly pass through the literal backslashes).

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • how can I run the following command in Python `/zhome/9e/b/129343/Maven/apache-maven-3.6.3/bin/mvn exec:java -Dexec.mainClass="org.matsim.contrib.ev.example.MyRunEvExample"` ? There is something wrong with the quotation marks. – user3474218 Apr 14 '21 at 13:47
  • Drop the quotes entirely; the shell would parse the command into exactly this, too. `subprocess.run(["/zhome/9e/b/129343/Maven/apache-maven-3.6.3/bin/mvn", "exec:java", "-Dexec.mainClass=org.matsim.contrib.ev.example.MyRunEvExample"])` and probably add `check=True` before the closing parenthesis. – tripleee Apr 14 '21 at 13:56
  • You can use `printf '%s\n' /zhome/9e/b/129343/Maven/apache-maven-3.6.3/bin/mvn exec:java -Dexec.mainClass="org.matsim.contrib.ev.example.MyRunEvExample"` to see this in action. Try the format string `'"%q"\n'` if you really want to see trailing spaces etc. – tripleee Apr 14 '21 at 13:59
0

To escape this you can use two different approaches. The first is just to escape all inner quotes like

subprocess.run([r"cd C:\Program Files\Java\jdk-11.0.10\bin\javaw.exe",
    r"\"@C:\Users\danie\IdeaProjects\EV_example\EV_example\matsim-libs\contribs\ev\.temp-ChargerLocation-args-1617369801473.txt\""])

The second - use for string single quotes, if you need double quotes in it. Python doesn't make any difference between single and double quotes to indicate strings.

subprocess.run([r'cd C:\Program Files\Java\jdk-11.0.10\bin\javaw.exe',
    r'"@C:\Users\danie\IdeaProjects\EV_example\EV_example\matsim-libs\contribs\ev\.temp-ChargerLocation-args-1617369801473.txt"'])
STerliakov
  • 4,983
  • 3
  • 15
  • 37