0

We have multiple bash commands like

cd /home/ ; ls -lrt abc* ; cp abc* /destination/ ; ....

we can run multiple commands in the bash as per above using ";"

can we run in the same way in python ? (something all the commands into a variable and execute using os or subprocess)

Is there a way to run all these commands in a single line in python

3
  • 1
    Of course. The argument to os.system() can be any shell command line, including multiple commands separated by ;. Commented Feb 22, 2022 at 11:15
  • @Barmar .. Is that something i can give in this way --- > os.system("ls lrt abc*; cp abc* /destination") Commented Feb 22, 2022 at 11:17
  • Ravi: Can't you just try it and see for yourself? Commented Feb 22, 2022 at 13:51

1 Answer 1

2

You can use the subprocess python library

import subprocess

command = "cd /home/ ; ls -lrt abc* ; cp abc* /destination/ ; ...."

ret = subprocess.run(command, capture_output=True, shell=True)


print(ret.stdout.decode())

https://docs.python.org/3/library/subprocess.html

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

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.