-1

I'm getting error when shell=False with sub process call but it's working when shell=True.

String = "geeks"
CriticalCmd = "/bin/sed -i /^cu\\:/c\\ '%s' a" %String.rstrip('s')
subprocess.call((replaceHashCmd),shell=False)
james
  • 39
  • 2
  • 7
  • 1
    Show the error message as properly formatted text in the question. – Michael Butscher Dec 12 '19 at 17:36
  • 1
    Error is: Traceback (most recent call last): File "test.py", line 21, in subprocess.call(replaceHashCmd,shell=False) File "python2.7/subprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() File "python2.7/subprocess.py", line 711, in init errread, errwrite) File "python2.7/subprocess.py", line 1327, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory – james Dec 12 '19 at 18:23
  • 1
    But when i change shell=True it works, but not working with shell=False – james Dec 12 '19 at 18:23

1 Answers1

0

It's propbably because you calling subprocess.call with a tuple of one string and not list of strings (as the arguments of the shell)

change: subprocess.call((replaceHashCmd),shell=False)

to: subprocess.call(replaceHashCmd.split(' '),shell=False)

.split allow me to cut the string into the argument that i pass to the command line, like doing this :'ls -l'.split() # ['ls', '-l'] which is all the args you passing to the command line

consider reading this post: link

Reznik
  • 2,663
  • 1
  • 11
  • 31
  • 1
    I passed as you mentioned but still getting same error. Traceback (most recent call last): File "test.py", line 21, in subprocess.call(replaceHashCmd,shell=False) File "python2.7/subprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() File "python2.7/subprocess.py", line 711, in __init__ errread, errwrite) File "python2.7/subprocess.py", line 1327, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory – james Dec 12 '19 at 18:22
  • 1
    But when i change shell=True it works, but not working with shell=False – james Dec 12 '19 at 18:23
  • 1
    @james also check this answer [link](https://stackoverflow.com/a/3172488/7434857) – Reznik Dec 12 '19 at 18:25
  • 1
    Yes it works..but can you tell me why you added 'split' – james Dec 12 '19 at 18:28
  • @james updated my answer, consider upvote my answer and mark it as correct if it the correct answer for you :) – Reznik Dec 12 '19 at 18:30
  • How to use ONTAPE command with subprocess.call with Shell = False import subprocess import shlex Cmd1 = "/bin/gunzip -c abc.gz" Cmd2 = "ontape -r -v -t STDIO" pip = subprocess.Popen(shlex.split(Cmd1),shell=False,stdout=subprocess.PIPE) RC = os.WEXITSTATUS(subprocess.call(shlex.split(Cmd2),shell=False,stdin=pip.stdout)) Getting Error with Shell = False, It works when Shell= True – james Dec 12 '19 at 19:59
  • @james its a question for another stackoverflow question, if you think my asnwer is the right one for this problem, mark it as currect and vote it up :) – Reznik Dec 13 '19 at 16:18