0

I am trying to run 2 bash commands sequentially in a for loop in Python. The loop is as given below:

for dataset in data:
        subprocess.call("cd " +dataset+'.matrix',shell=True)
        subprocess.call("cat part-r-* > combined_output", shell=True)

However, in this way, each command is being considered independently of each other. I need them to execute one after the other. I don't know how to use the subprocess module well (I also tried with os.system). I went through some documentation online but they weren't really useful. Any help in this would be appreciated. thanks in advance!

2 Answers 2

3

I don't believe they're running asynchronously (at the same time), just that they're in different subprocesses which each inherit the parent process' working directory. So it is waiting for the cd to complete, but the subprocess which has cd'd then disappears.

Thankfully, in this case you can tell the call to operate in a different directory with the cwd parameter, so you don't need your cd:

for dataset in data:
    subprocess.call("cat part-r-* > combined_output",shell=True,cwd=dataset+'.matrix')
Sign up to request clarification or add additional context in comments.

Comments

0

An example with os.system:

import os
os.system("echo aaa > ~/SOMEFILE1111; cd ~/; cat SOMEFILE1111; rm SOMEFILE1111")

and one with subprocess.call:

import subprocess
cmds = [ "echo aaa > ~/SOMEFILE1111", "cd ~/",
        "cat SOMEFILE1111", "rm SOMEFILE1111"]
subprocess.call(";".join(cmds), shell=True)

2 Comments

is it OK if I write like this: os.system("cd " +dataset+ '.matrix';"cat part-r-* > combined_output")
It should be like this: "cd " +dataset+ '.matrix; cat part-r-* > combined_output'

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.