Python Forum
using a shell script within python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using a shell script within python
#3
Should not be using 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())
Reply


Messages In This Thread
using a shell script within python - by Krszt - Oct-10-2018, 09:26 AM
RE: using a shell script within python - by snippsat - Oct-10-2018, 11:00 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help creating shell scrip for python file marciokoko 10 1,380 Sep-16-2023, 09:46 PM
Last Post: snippsat
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,276 Jun-29-2023, 11:57 AM
Last Post: gologica
  Launch Python IDLE Shell from terminal Pavel_47 5 1,259 Feb-17-2023, 02:53 PM
Last Post: Pavel_47
  batch file for running python scipt in Windows shell MaartenRo 2 1,901 Jan-21-2022, 02:36 PM
Last Post: MaartenRo
  cant use ping, sudo or other commands in remote shell script. throwaway34 7 3,618 May-17-2021, 11:29 AM
Last Post: throwaway34
  How to make a Python program run in a dos shell (cmd) Pedroski55 2 2,333 Nov-09-2020, 10:17 AM
Last Post: DeaD_EyE
Bug Python Shell 3.9.0 - Issue with indentation Earis 17 6,661 Oct-31-2020, 07:00 AM
Last Post: Earis
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,938 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,313 May-28-2020, 05:27 PM
Last Post: micseydel
  Python "Terminal" vs "Shell" SectionProperties 2 2,677 Apr-10-2020, 08:36 AM
Last Post: SectionProperties

Forum Jump:

User Panel Messages

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