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

David bouncingcats at gmail.com
Fri Aug 9 00:47:56 EDT 2013


On 9 August 2013 14:11, Adam Mercer <ramercer at gmail.com> 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:

[...]

At a quick glance, I have a couple of suggestions.

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

sudo doesn't work like this. It doesn't read from standard input. You
need to supply the command as an argument to sudo. Get the sudo syntax
correct by learning to use it in a shell (eg terminal running bash )
before trying to use it from python code.

Also, I think that passing the pipe character '|' as an argument to
Popen is not the correct way to use pipes.

So, if you figure out how to use sudo without '|' you will solve both
these issues.



More information about the Python-list mailing list