I am calling a shell script file through a python command. Filename1 is a variable I have created in my standalone python program storing the actual value of filename. How do I write it to execute the 2nd statement below as Filename variable doesn't work here. If I keep the actual filename, the shell script file gets executed through second statement.

I need to automate this and cannot hardcode the actual filename. Thus, I want if by any chance the variable can be accessed in 2nd statement.

import os
os.system('sh uploadPDFContentFile.sh Filename1')

Better use subprocess module.

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function.

But, you may use string substitution:

call_with_args = "./script2.py '%s' '%s'" % (str(arg1), str(arg2))

Or string format

>>> '{0}, {1}, {2}'.format('a', 'b', 'c')
'a, b, c'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.