Execute Commands Sequentially in Python?

6,391

Solution 1

There is an easy way to execute a sequence of commands.

Use the following in subprocess.Popen

"command1; command2; command3"

Or, if you're stuck with windows, you have several choices.

  • Create a temporary ".BAT" file, and provide this to subprocess.Popen

  • Create a sequence of commands with "\n" separators in a single long string.

Use """s, like this.

"""
command1
command2
command3
"""

Or, if you must do things piecemeal, you have to do something like this.

class Command( object ):
    def __init__( self, text ):
        self.text = text
    def execute( self ):
        self.proc= subprocess.Popen( ... self.text ... )
        self.proc.wait()

class CommandSequence( Command ):
    def __init__( self, *steps ):
        self.steps = steps
    def execute( self ):
        for s in self.steps:
            s.execute()

That will allow you to build a sequence of commands.

Solution 2

To do that, you would have to:

  • supply the shell=True argument in the subprocess.Popen call, and
  • separate the commands with:
    • ; if running under a *nix shell (bash, ash, sh, ksh, csh, tcsh, zsh etc)
    • & if running under the cmd.exe of Windows

Solution 3

Finding 'bar' in every file whose name contains 'foo':

from subprocess import Popen, PIPE
find_process = Popen(['find', '-iname', '*foo*'], stdout=PIPE)
grep_process = Popen(['xargs', 'grep', 'bar'], stdin=find_process.stdout, stdout=PIPE)
out, err = grep_process.communicate()

'out' and 'err' are string objects containing the standard output and, eventually, the error output.

Solution 4

Yes, the subprocess.Popen() function supports a cwd keyword argument, with which you can set the directory it runs the process in.

I guess the first step, the shell, is not needed, if all you want is to run ls, there's no need to run it through a shell.

Of course, you could also just pass the desired directory as an argument to ls.

Update: it might be worth noting that for typical shells, cd is implemented in the shell itself, it is not an external command on disk. This is because it needs to change the process' current directory, which must be done from within the process. Since commands run as child processed, spawned by the shell, they cannot do this.

Share:
6,391
Lakshmi Priya
Author by

Lakshmi Priya

Updated on July 31, 2022

Comments

  • Lakshmi Priya
    Lakshmi Priya almost 2 years

    Here i want to Accept button. Here is the HTML.

    <div class="friend-request no-pad ng-scope" ng-if="notifications.friendInvites.length > 0">
    <p class="rem-head mzero small">
    <div class="reminder-lst lst-box ng-scope" ng-repeat="friendInvite in notifications.friendInvites | limitTo:limit">
    <span class="img-frame img-circle">
    <span class="pull-left rem-detail-a">
    <a class="pull-left rem-detail-a pzero" href="friend#/friends/friendprofile/b6c70e4f-bfe1-440d-836c-2e8fdc88540e">
    <span class="frndact pull-right">
    <a class="ignore" ng-click="ignoreNotification(friendInvite, 'friend')" href="javascript:void(0)">
    <a class="accept" ng-click="acceptNotification(friendInvite, 'friend')" href="javascript:void(0)">
    <i class="fa fa-lg fa-check-circle green"></i>
    </a>
    

    I have tried using below xpath but not working. Can anyone plz help me?

    @FindBy(xpath=".//a[ng-click='acceptNotification(friendInvite, 'friend')']/preceding-sibling::i[@css='.fa.fa-lg.fa-check-circle.green']").
    

    Thanks in advance

    • TT.
      TT. over 8 years
      //a[@ng-click='acceptNotification(friendInvite, 'friend')'] ... (notice the missing @ in your XPath). Also what specific element did you want to select? Because I'm not seeing what that element is looking at your XPath.