using a shell script within python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: using a shell script within python (/thread-13330.html) |
using a shell script within python - Krszt - Oct-10-2018 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") RE: using a shell script within python - volcano63 - Oct-10-2018 (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. 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
RE: using a shell script within python - snippsat - Oct-10-2018 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()) |