1

In subprocess module in python,

 subprocess.Popen(
            (
                "git",
                "add",
                "-A",
                "&&",
                "git",
                "commit",
                "-m",
                commit_message
            ),
            cwd=path,
        )

does not work. However if I split it into

 subprocess.Popen(( "git", "add","-A") , cwd=path)

and

subprocess.Popen(( "git", "commit","-m" , "yeah") , cwd=path)

it works. How do I insert "&&" in the middle? Thanks.

petezurich
  • 9,280
  • 9
  • 43
  • 57
eyah
  • 111
  • 1
  • 8
  • 1
    The various `subprocess` functions run *a single command*. If you put a `&&` in the middle, it would simply be another parameter to that one command. You do have the option of using `shell=True`, in which case the command that gets run is a shell; that shell is capable of interpreting an arbitrarily-complicated command line, including multiple commands and redirections. Note that in this case, the command line should be provided as a single string, splitting it into a list is unnecessary. – jasonharper May 24 '22 at 18:43

2 Answers2

1

The && logical operator is something that the shell (e.g., bash) understands. However, you're executing git which doesn't understand what && means. Explained another way, you're entering the arguments as if you were typing them into a terminal. subprocess.Popen doesn't invoke the shell by default and instead invokes the exec system call with the specified arguments. So, you're telling it to run git with the arguments add, -A (so far, so good), && (git doesn't understand this), git, -m, and yeah.

To execute this as a shell command, you need to add shell=True to subprocess.Popen. Once you do that, you can just type out the command you want as a single string:

subprocess.Popen('git add -A && git commit -m', shell=true, cwd=path)
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
1

Only a shell can interpet &&, it's not program or an argument

You can pass shell=True to subprocess.Popen to run your command in a mini-shell like environment, see docs here

ahmed
  • 5,430
  • 1
  • 20
  • 36