Python Forum
Number of cmds inside subprocess
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Number of cmds inside subprocess
#1
Hi, I am trying to run more than 5 or 6 shell commands from my pyhton script.

Currently my psuedo code is below.

try:
     _ = subprocess.check_output(cmd1, shell=True, stderr=subprocess.STDOUT)
     _ = subprocess.check_output(cmd2, shell=True, stderr=subprocess.STDOUT)
     _ = subprocess.check_output(cmd3, shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
      print(e.returncode)
Can someone correct me if this is not right way to do? Thanks.
Reply
#2
I think you should use
_ = subprocess.call(cmd1, shell=True, stderr=subprocess.STDOUT)
Reply
#3
(Sep-14-2017, 06:35 AM)Sagar Wrote: I think you should use
_ = subprocess.call(cmd1, shell=True, stderr=subprocess.STDOUT)

Thanks Sagar for your reply.
If I have many cmd calls, Can I call them in one try statement as mentioned in my first post? Or there is a better way of implementing these cmd steps? Thanks.
Reply
#4
I have written a small code snippet. You could scale up and improve that code. Please let me known what you have done so I could learn from you also.
The code is as follows:
def subprocess_cmd(command):
    try:
        process = subprocess.call(command,stdout=subprocess.PIPE, shell=True)
        
    except subprocess.CalledProcessError as e:
      print(e.returncode)

subprocess_cmd('ls -l; echo b')
Reply
#5
(Sep-14-2017, 07:17 AM)Sagar Wrote: I have written a small code snippet. You could scale up and improve that code. Please let me known what you have done so I could learn from you also.
The code is as follows:
def subprocess_cmd(command):
    try:
        process = subprocess.call(command,stdout=subprocess.PIPE, shell=True)
        
    except subprocess.CalledProcessError as e:
      print(e.returncode)

subprocess_cmd('ls -l; echo b')

Got it. Thanks.
I will write a function to call each subprocess.
Reply
#6
(Sep-14-2017, 07:23 AM)pannis Wrote: I will write a function to call each subprocess.


So, that means you don't get it.  Don't write a function for each subprocess, just use that one function, over and over.  If you have more than one function defined, you did it wrong.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open windows cmd prompt and run cmds with python Extra 3 1,439 Jul-14-2022, 06:07 AM
Last Post: Gribouillis
  Putting many coordinates number inside codes fyec 1 2,201 Jun-07-2018, 01:09 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020