LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Python: Using strings to run commands in os.system('') (https://www.linuxquestions.org/questions/programming-9/python-using-strings-to-run-commands-in-os-system-887416/)

MetaMan 06-20-2011 07: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:
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! :D

indienick 06-20-2011 08:24 PM

Maybe try using formatting directives:
Code:

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

grail 06-20-2011 09:16 PM

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

MetaMan 06-20-2011 09: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:
Code:

cto = self.copy_to.get_text
cfrom = self.copy_from.get_text


ntubski 06-20-2011 10:23 PM

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


All times are GMT -5. The time now is 04:15 AM.