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
#1
I want to use a C-shell script within a python program, which works with two arguments.

os.system("recall 20170121 ../E725.txt xy 1")

But I want to use it for a stack, so declared the variables like below, but when I call them within the script it gives an error, that the input file doesn' exist. How can I call the variables?

date_ID=(filename[17:25])
fullpath = '../%s' % (filename)
os.system("import_S1_TOPS_modified $date_ID $fullpath vv 1")
Reply
#2
(Oct-10-2018, 09:26 AM)Krszt Wrote: I want to use a C-shell script within a python program, which works with two arguments.

os.system("recall 20170121 ../E725.txt xy 1")

But I want to use it for a stack, so declared the variables like below, but when I call them within the script it gives an error, that the input file doesn' exist. How can I call the variables?

date_ID=(filename[17:25])
fullpath = '../%s' % (filename)
os.system("import_S1_TOPS_modified $date_ID $fullpath vv 1")

As far as Python interpreter is concerned, $date_ID and $fullpath in your command line are just string literals within a string. In order to place values within the command string
  • If you are using version >= 3.6 - modify your string as
    f"import_S1_TOPS_modified ${date_ID} ${fullpath} vv 1"
  • if not
    "import_S1_TOPS_modified ${} ${} vv 1".format(date_ID, fullpath)
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help creating shell scrip for python file marciokoko 10 1,254 Sep-16-2023, 09:46 PM
Last Post: snippsat
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,003 Jun-29-2023, 11:57 AM
Last Post: gologica
  Launch Python IDLE Shell from terminal Pavel_47 5 1,143 Feb-17-2023, 02:53 PM
Last Post: Pavel_47
  batch file for running python scipt in Windows shell MaartenRo 2 1,824 Jan-21-2022, 02:36 PM
Last Post: MaartenRo
  cant use ping, sudo or other commands in remote shell script. throwaway34 7 3,532 May-17-2021, 11:29 AM
Last Post: throwaway34
  How to make a Python program run in a dos shell (cmd) Pedroski55 2 2,255 Nov-09-2020, 10:17 AM
Last Post: DeaD_EyE
Bug Python Shell 3.9.0 - Issue with indentation Earis 17 6,426 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,786 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,247 May-28-2020, 05:27 PM
Last Post: micseydel
  Python "Terminal" vs "Shell" SectionProperties 2 2,617 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