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

Chris Angelico rosuav at gmail.com
Fri Aug 9 11:05:17 EDT 2013


On Fri, Aug 9, 2013 at 2:50 PM, Adam Mercer <ramercer at gmail.com> wrote:
> On Fri, Aug 9, 2013 at 8:42 AM, Antoon Pardon
> <antoon.pardon at rece.vub.ac.be> wrote:
>
>> That is probably beside the point. I suspect Adam is just giving a
>> minimal example to show the kind of thing he is trying to do.
>>
>> Nit picking the specific example instead of advising on the problem
>> is likely to be less than helpful.
>
> It is a simplified example, but in this case the nitpicking was very
> helpful. Caused me to think about the problem differently, and
> therefore come up with a neater solution.

It wasn't nitpicking so much as suggesting a more Pythonic way to do
things. It's entirely plausible that the response would have been
"Actually, the real command is more complicated than 'echo' so I
really do need a pipe", but it's *very* common in shell scripts to
call on an external process to do something that in other languages is
a builtin. For instance, how do you get the name of the parent of your
current directory in bash? Something along the lines of:

parent=$(dirname $(pwd$)$)

which executes two external commands. In Python, it would be:

os.path.dirname(os.getcwd())

which is two internal function calls. It's just one of the things to
consider when porting code from one language to another - I wouldn't
often use a list comprehension in bash, and I wouldn't often want a
pipe between two external processes in Python.

ChrisA



More information about the Python-list mailing list