0

Python3 subprocess call not working

import subprocess 
subprocess.call("sudo nautilus")
Rupali Sachan
  • 23
  • 1
  • 2

3 Answers3

1

Try this one:

subprocess.call(["sudo", "nautilus"])
GLR
  • 1,070
  • 1
  • 11
  • 29
  • +1 for suggesting only the good-practice approach, *grumph* for ignoring "Answer well-asked questions" from [How to Answer](https://stackoverflow.com/help/how-to-answer), noting that questions that have already been asked and answered many times before should not be answered again. – Charles Duffy Sep 20 '17 at 13:58
0

Try setting shell=True, so:

subprocess.call("sudo nautilus",shell=True)
Milan Velebit
  • 1,933
  • 2
  • 15
  • 32
  • And please post the traceback next time. – Milan Velebit Sep 20 '17 at 13:47
  • `shell=True` makes your code `subprocess.call(["sh", "-c", "sudo nautilus"])` -- `shell=True` (on UNIX) literally appends the arguments to the hardcoded list `['sh', '-c']`, adding an extra indirection layer, with all the inefficiency that implies. (Since that indirection layer parses the string as code, it also has security impact if values are substituted into the command line being invoked). – Charles Duffy Sep 20 '17 at 13:53
0

You have to use the shell = true to make it work in the shell

 import subprocess 
 subprocess.call("sudo nautilus", shell=True)

OR u can output it a s a list

 subprocess.call(["sudo", "nautilus"])
Sumit
  • 486
  • 4
  • 16
  • `shell=True` makes your code `subprocess.call(["sh", "-c", "sudo nautilus"])` -- `shell=True` (on UNIX) literally appends the arguments to the hardcoded list `['sh', '-c']`, adding an extra indirection layer, with all the inefficiency that implies. (Since that indirection layer parses the string as code, it also has security impact if values are substituted into the command line being invoked). – Charles Duffy Sep 20 '17 at 13:52