Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use python variable in os.system? [duplicate]

Tags:

python

I am creating small console script in python, and I will like to put cowsay command in it, but cow says name of the variable, where the string is, not the string inside the variable. How I can get the cow to say string inside the variable?

if (command == 'cow'):
    word = raw_input('What does the cow say?  ')
    os.system('cowsay word')
like image 877
Aleksi Väisänen Avatar asked Nov 25 '14 14:11

Aleksi Väisänen


People also ask

Can we use same variable in Python?

In Python, we may reuse the same variable to store values of any type. A variable is similar to the memory functionality found in most calculators, in that it holds one value which can be retrieved many times, and that storing a new value erases the old.

What does OS system return in Python?

The os. system() function executes a command, prints any output of the command to the console, and returns the exit code of the command.

Can two variables have the same name Python?

You don't have two variables with the same name. You are re-assigning the variable a new value. I'm curious what your background is. There are languages that only allow names to be bound once (or that use different syntax for initial binding and rebinding), but none of the "mainstream" ones work that way.


2 Answers

lazy solution is to simply concatenate the word:

>>> import os
>>> word="moo"
>>> os.system('cowsay ' + word)
 _____ 
< moo >
 ----- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
0

BUT you should not do this. What if the user inputs moo; rm -rf /? guess what will happen. Also, word="$(cat /etc/passwd)" and word="$aliases" or words with backticks will yield non-expected results.

You should use the Subprocess module, which takes care of escaping shell args and constructing the call:

>>> import subprocess
>>> subprocess.Popen(['cowsay', word])
<subprocess.Popen object at 0x7fe8c7656c18>
>>>  _____ 
< moo >
 ----- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Use .communicate() for simple invocations, as described in the docs or as in the example below. And now you don't have to worry about injections:

>>> word="$(cat /etc/passwd)"
>>> stdout, stderr = subprocess.Popen(
                     ['cowsay', word]).communicate()
 ____________________ 
< $(cat /etc/passwd) >
 -------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
like image 84
ch3ka Avatar answered Sep 23 '22 08:09

ch3ka


You could use format to construct the string

os.system('cowsay {}'.format(word))

Or simple string concatenation

os.system('cowsay ' + word)

But I prefer the former, especially if the string get more complicated.

like image 21
Cory Kramer Avatar answered Sep 25 '22 08:09

Cory Kramer