2

I am trying to run to cmd code using python.

p = 'calcifer --config="C:\\Users\\yilin.chen\\Desktop\\pythonminingstuff\\calcifer\\calcifer\\pipelines\\run_config.yaml"'
subprocess.call('cd C:\\Program Files (x86)\\Olympus\\Vanta\\bin', shell = True)
subprocess.call(p, shell = True)

And later I found that, these two line of code should be ran together. So I tried

commands = """ SET foo=1 | SET foo=2 | echo %foo% """
b = subprocess.check_output(commands, shell=True)
print(b.decode('ascii'))

Which is posted here as a guide https://mail.python.org/pipermail/tutor/2013-January/093474.html But it doesn't work for me. The code above only executes the lasty line which prints a %foo%.If I copy and paste the orirginal code, it only prints 'hello'.

Any thoughts? I appreciate your help.

Drake .C
  • 332
  • 4
  • 16

2 Answers2

2

It seems like the first command you are trying to execute is to cd into a directory, which can be achieved by setting the cwd parameter in subprocess.call.

To give you a little example, I copied the echo binary to /Users/Samuel/tmp/eecchhoo. If I try to go in the directory, then call the binary in two subprocess calls, I will have a failure, as you described:

>>> import subprocess
>>> subprocess.call('cd /Users/Samuel/tmp', shell=True)
0
>>> subprocess.call('./eecchhoo helloworld', shell=True)
/bin/sh: ./eecchhoo: No such file or directory
127

However I can make the call succeed by setting the cwd parameter to the value I want:

>>> import subprocess
>>> subprocess.call('./eecchhoo helloworld', shell=True, cwd='/Users/Samuel/tmp')
helloworld
0

If you need to run other commands (not only to change the working directory), you can refer to this answer: Python: execute cat subprocess in parallel.

Samuel Dion-Girardeau
  • 2,790
  • 1
  • 29
  • 37
  • It's amazing. But if I am going to do two 'echo', what should I do? – Drake .C May 10 '18 at 13:32
  • 1
    @ChenLin You can use `subprocess.call` like you did, but you need to separate the different commands correctly. On my platform (Mac OS), I can use `&&` ("and" operator) or `;` ("or" operator): `subprocess.call('echo hello && echo world', shell=True)` and `subprocess.call('echo hello ; echo world', shell=True)` both work. – Samuel Dion-Girardeau May 10 '18 at 19:08
  • 1
    I tried and it worked, thank you so much for your help. I really appreciate it. – Drake .C May 11 '18 at 13:11
1

Use the '&&' operator.
For your example:

p = 'calcifer --config="C:\\Users\\yilin.chen\\Desktop\\pythonminingstuff\\calcifer\\calcifer\\pipelines\\run_config.yaml"'
subprocess.call('cd C:\\Program Files (x86)\\Olympus\\Vanta\\bin && ' + p, shell = True)
subprocess.call(p, shell = True)


You can also use os.system(command) to do the exact same thing:

import os
p = 'calcifer --config="C:\\Users\\yilin.chen\\Desktop\\pythonminingstuff\\calcifer\\calcifer\\pipelines\\run_config.yaml"'
os.system('cd C:\\Program Files (x86)\\Olympus\\Vanta\\bin && ' + p)



You can read more about it here.

Elyasaf755
  • 2,239
  • 18
  • 24