Should not be using
Ideally is better/safer to pass in a list.
but the method with list when
os.system()
,subprocess replace it a better/safer way.Ideally is better/safer to pass in a list.
import subprocess subprocess.run(['ls', '-l'])Passing in a string has to use
shell=True
import subprocess subprocess.run('ls -l', shell=True)So as shown bye @volcano63 can pass in using f-string or format(),
but the method with list when
shell=False
is advisable. import subprocess arg = '-l' subprocess.run(f'ls {arg}', shell=True)The subprocess has a lot stuff that make it better,even
run
(that replace call) can now also catch output.import subprocess out = subprocess.run(['ls', '-l'], capture_output=True) print(out.stdout.decode())