Using sudo to write to a file as root from a script

Nobody nobody at nowhere.com
Fri Aug 9 16:12:20 EDT 2013


On Thu, 08 Aug 2013 23:11:09 -0500, Adam Mercer wrote:

> I'm trying to write a script that writes some content to a file root
> through sudo, but it's not working at all. I am using:

>   command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file]

You can't create a pipeline like this. All of the list elements after the
first will be passed as arguments to "echo".

Try:

  command = ['sudo', 'tee', config_file]
  p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  out, _ = p.communicate('channel')




More information about the Python-list mailing list