Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-26-2007, 12:56 PM
|
#1
|
|
Senior Member
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,167
|
python, os.system() function. howto use python variables?
i would like to do something like this in python:
Code:
myCoolVariable="some_string"
os.system("echo myCoolVariable")
however, the system function views myCoolVariable as a string literal, not as a python variable. What do I do to get around this?
Thank you,
Jameson
|
|
|
|
07-26-2007, 01:35 PM
|
#2
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
It needs to be one of these forms:
Code:
>>> os.system('echo "asdg"')
asdg
0
>>> os.system("echo 'asdgwere'")
asdgwere
0
The outer quotes define the string to be passed to the shell, and the inner quotes define what gets passed to echo.
|
|
|
|
07-26-2007, 01:56 PM
|
#3
|
|
Senior Member
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,167
Original Poster
|
That is just printing out the string literals you are passing to echo - i am looking to resolve the python varaible myCoolVariable before the execution of echo in the sysem() call.
For example, I would like:
Code:
$ python
>>>hamburger="potato"
>>>import os
>>>os.system("echo 'hamburger'")
to somehow produce
in this case i could just write "potato" directly, but things get stickier when i want to say
Code:
hamburger=os.system('echo $MY_SHELL_VAR | sed "s/longsed/replacement/g"')
, or something of that flavour
Last edited by jhwilliams; 07-26-2007 at 02:01 PM.
|
|
|
|
07-26-2007, 04:14 PM
|
#4
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
Bingo!!!
Code:
>>> string="python is cool"
>>> cmd="echo "+string
>>> os.system(cmd)
python is cool
0
>>>
|
|
|
|
07-26-2007, 05:18 PM
|
#5
|
|
Senior Member
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,167
Original Poster
|
Thanks pixellany!
|
|
|
|
07-28-2007, 01:56 AM
|
#6
|
|
LQ Newbie
Registered: Jul 2007
Location: Bristol UK
Distribution: RedHat, Cygwin
Posts: 14
Rep:
|
Or do string interpolation:
Code:
bash$ python
Python 2.4.3 (#1, May 18 2006, 07:40:45)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from os import system
>>> food = 'spam'
>>> system('echo %(food)s' % locals())
spam
0
>>>
- Paddy.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:45 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|