Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 06-20-2011, 08:24 PM   #1
Registered: Apr 2011
Distribution: Arch Linux :D
Posts: 50

Rep: Reputation: 0
Question Python: Using strings to run commands in os.system('')

I was zsync-ing the latest Ubuntu 11.10 Alpha and thought I'd make a little GUI for it as a small project. The gui is set up, I just need to figure out how to run zsync with content from to variables, cto and cfrom. I tried the following code:
os.system('zsync -i ' + cto + ' ' + cfrom)
which gives me the error:
Traceback (most recent call last):
  File "", line 45, in start_button_clicked
    os.system('zsync -i ' + cto + ' ' + cfrom)
TypeError: cannot concatenate 'str' and 'builtin_function_or_method' objects
Does anyone know how to fix this?

Old 06-20-2011, 09:24 PM   #2
Senior Member
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Maybe try using formatting directives:
os.system('zsync -i {0} {1}'.format(cto, cfrom))
Old 06-20-2011, 10:16 PM   #3
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 8,307

Rep: Reputation: 2251Reputation: 2251Reputation: 2251Reputation: 2251Reputation: 2251Reputation: 2251Reputation: 2251Reputation: 2251Reputation: 2251Reputation: 2251Reputation: 2251
What if you concatenate into a single string and pass that to os.system()?
Old 06-20-2011, 10:42 PM   #4
Registered: Apr 2011
Distribution: Arch Linux :D
Posts: 50

Original Poster
Rep: Reputation: 0
Exclamation No Dice

Wait a minute, it must be an issue with how I'm assigning text to variables...

This is wrong, it't it:
cto = self.copy_to.get_text
cfrom = self.copy_from.get_text

Last edited by MetaMan; 06-20-2011 at 10:55 PM.
Old 06-20-2011, 11:23 PM   #5
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,820

Rep: Reputation: 999Reputation: 999Reputation: 999Reputation: 999Reputation: 999Reputation: 999Reputation: 999Reputation: 999
I guess get_text is a function, so you have to call it to get the text:
cto = self.copy_to.get_text()
Also, I would recommend using the subprocess module functions to pass the parameters separately, that way even if the arguments have spaces or other special characters in them, you won't get weird behaviour:
Code:['zsync', '-i', cto, cfrom])
1 members found this post helpful.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] python: how to pass output of system commands to variable daweefolk Programming 1 01-25-2011 03:11 PM
Python: replacing strings in a file tonya11en Programming 2 05-19-2009 07:52 PM
how to run system commands from jsp page robin singh Linux - Newbie 8 03-07-2009 11:28 AM
Run some commands on system shutdown luizrolla Linux - Newbie 1 07-12-2004 08:42 PM
Using letters/strings to assign variables in python Colonel Panic Programming 0 09-14-2001 11:13 PM

All times are GMT -5. The time now is 08:44 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration