-   Programming (
-   -   Python: Using strings to run commands in os.system('') (

MetaMan 06-20-2011 08:24 PM

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?

Thanks! :D

indienick 06-20-2011 09:24 PM

Maybe try using formatting directives:

os.system('zsync -i {0} {1}'.format(cto, cfrom))

grail 06-20-2011 10:16 PM

What if you concatenate into a single string and pass that to os.system()?

MetaMan 06-20-2011 10:42 PM

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

ntubski 06-20-2011 11:23 PM

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])

All times are GMT -5. The time now is 01:02 AM.