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.

1
  • 1
    I presume you are assuming that the | character is a command separator? Commented May 9, 2018 at 21:36

2 Answers 2

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.

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

3 Comments

It's amazing. But if I am going to do two 'echo', what should I do?
@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.
I tried and it worked, thank you so much for your help. I really appreciate it.
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.

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.