1

I am trying to execute a windows command from python.

"C:\Program Files (x86)\IntelSWTools\VTune Amplifier 2018\bin64\amplxe-cl" -collect general-exploration-0 -app-working-dir "C:\Program Files (x86)\Google\Chrome\Application" --duration 180 -- "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

python is not able to parse this part

C:\Program Files (x86)\Google\Chrome\Application

I tried breaking up the query to narrow down the issue and tried using raw strings as well and forward slashes. Heres my code

import os
a =r'"C:/Program Files (x86)/IntelSWTools/VTune Amplifier 2018/bin64/amplxe-cl.exe"'
b=r' -collect general-exploration-0 -app-working-dir '
c= r"C:/Program Files (x86)/Google/Chrome/Application"
d=r' --duration 180 -- '
e=r'"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"'

z = a + b + c  + d + e 
os.system(z)

I am getting Error as 'C:/Program' is not recognized as an internal or external command, operable program or batch file.

Which is due to 'c' string in code.

Anchal Raheja
  • 29
  • 2
  • 8

3 Answers3

1

Python passes the command to Command Prompt as if you ran it with cmd.exe /C <command>. Command Prompt decides whether to keep the first and last double quotation marks based on the criteria that can be seen by running cmd.exe /?.

Printing the value of z, one gets "C:/Program Files (x86)/IntelSWTools/VTune Amplifier 2018/bin64/amplxe-cl.exe" -collect general-exploration-0 -app-working-dir C:/Program Files (x86)/Google/Chrome/Application --duration 180 -- "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe". This string does not pass all of the tests required to keep its quotation marks, so the first step is to wrap the whole thing in another pair of quotation marks: z = '"' + a + b + c + d + e + '"'.

However, the argument to app-working-dir is also unquoted, so c needs to be changed to c= r'"C:/Program Files (x86)/Google/Chrome/Application"'. Now print z will return ""C:/Program Files (x86)/IntelSWTools/VTune Amplifier 2018/bin64/amplxe-cl.exe" -collect general-exploration-0 -app-working-dir "C:/Program Files (x86)/Google/Chrome/Application" --duration 180 -- "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"", and os.system(z) will work, as long as the files and directories in the command exist.

An alternative approach is to use 8.3 short filenames and avoid quotation marks altogether, such that z is C:/PROGRA~2/IntelSWTools/VTUNEA~1/bin64/amplxe-cl.exe -collect general-exploration-0 -app-working-dir C:/PROGRA~2/Google/Chrome/Application --duration 180 -- C:/PROGRA~2/Google/Chrome/Application/chrome.exe. The short names here (like PROGRA~2) are my guesses on what they might be; run the command DIR /X in the parent directory to see what the actual short name is on a specific system.

Joshua Issac
  • 33
  • 1
  • 4
0

The issue you are facing is because of the fact that \ is acting as an escape character in C:\Program Files (x86)\Google\Chrome\Application\chrome.exe.

Your Solution can be resolved by following the instructions on Windows path in Python

Let me know if you still have issues, cheers!

Haris Nadeem
  • 1,322
  • 11
  • 24
  • If you mean using forward slash in the code I am doing that. – Anchal Raheja Apr 01 '18 at 04:31
  • I think I see where the issue is, you typed `e=r'"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"'` instead of `e=r"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"` – Haris Nadeem Apr 01 '18 at 04:39
  • If i try to execute only 'e', It works fine. I think 'c ' is creating error – Anchal Raheja Apr 01 '18 at 04:43
  • try putting in `--app-working-dir` instead of `-app-working-dir` then try it and let me know – Haris Nadeem Apr 01 '18 at 04:49
  • So from what I understand, the issue lies in python not understanding what `r"C:/Program Files (x86)/Google/Chrome/Application"` is. So when you pass it to the OS it thinks that it is part of a new command and not part of the argument. Which is why I suggested `--`. That's why it complains that `'C:/Program' is not recognized as an internal or external command, operable program or batch file.` Did you try running the command in cmd prompt to make sure that it runs there correctly? – Haris Nadeem Apr 01 '18 at 04:54
  • If that doesn't work try putting an `=` for whichever argument it belongs to, like `--app-working-dir=C:/Program Files (x86)/Google/Chrome/Application` – Haris Nadeem Apr 01 '18 at 04:55
  • tried both. is there any other better way to execute the command rather than os.system() ? – Anchal Raheja Apr 01 '18 at 05:28
  • I created a .bat file with the command and calling that from python, for now. – Anchal Raheja Apr 01 '18 at 05:37
  • good point! let me post that as an answer since not enough space here – Haris Nadeem Apr 01 '18 at 05:51
0

If you were to use the subprocess library, you could spawn a thread to do the job and continue working in parallel or wait on the thread. This is how you would do it (or something like this).

import subprocess

cmd = ["C:/Program Files (x86)/IntelSWTools/VTune Amplifier 2018/bin64/amplxe-cl.exe", "-collect general-exploration-0 -app-working-dir", "C:\Program Files (x86)\Google\Chrome\Application", "--duration 180 --", "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"]

p = subprocess.Popen(cmd, shell = True, stdout=subprocess.PIPE)

if p.wait() > 0:
    print "Error"
else:
    print "Success"

Or

try:
    p = subprocess.check_output(cmd, shell=True)
except Exception as e:
    print "Error: ", e
Haris Nadeem
  • 1,322
  • 11
  • 24