LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 06-20-2011, 07:24 PM   #1
MetaMan
Member
 
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:
Code:
os.system('zsync -i ' + cto + ' ' + cfrom)
which gives me the error:
Code:
Traceback (most recent call last):
  File "zsync.py", 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?

Thanks!
 
Old 06-20-2011, 08:24 PM   #2
indienick
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:
Code:
os.system('zsync -i {0} {1}'.format(cto, cfrom))
 
Old 06-20-2011, 09:16 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,518

Rep: Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896
What if you concatenate into a single string and pass that to os.system()?
 
Old 06-20-2011, 09:42 PM   #4
MetaMan
Member
 
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:
Code:
cto = self.copy_to.get_text
cfrom = self.copy_from.get_text

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

Rep: Reputation: 844Reputation: 844Reputation: 844Reputation: 844Reputation: 844Reputation: 844Reputation: 844
I guess get_text is a function, so you have to call it to get the text:
Code:
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:
subprocess.call(['zsync', '-i', cto, cfrom])
 
1 members found this post helpful.
  


Reply

Tags
python


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 02:11 PM
Python: replacing strings in a file tonya11en Programming 2 05-19-2009 06:52 PM
how to run system commands from jsp page robin singh Linux - Newbie 8 03-07-2009 10:28 AM
Run some commands on system shutdown luizrolla Linux - Newbie 1 07-12-2004 07:42 PM
Using letters/strings to assign variables in python Colonel Panic Programming 0 09-14-2001 10:13 PM


All times are GMT -5. The time now is 09:49 PM.

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