On 2/19/11 1:44 AM, Ganesh Kumar wrote:
os.system("ls -l") & root.destroy
"&" here doesn't do what you think it does. Its a bitwise AND operator,
which is not the same thing as you may be expecting from other languages.
In Python, you'd do something more like:
os.system("ls -l") and root.destroy()
But that's bad Python. First, let's look at what os.system actually returns:
system(command) -> exit_status
Execute the command (a string) in a subshell.
Now, an exit status of 0 is actually generally considered a success,
while 1-200odd is a failure.
Now, if you want to run 'root.destroy()' if and only if the 'ls -l'
command fails, you could do the above with an 'and'. Python DOES
short-circuit its logical AND's, so the root.destroy() will NOT be run
if ls -l succeeds (returns 0)... but really.
That's _very_ cryptic.
Its much better to do:
if os.system("ls -l") != 0:
(You could simply say 'if not os.system("ls -l")', but in this context
where the more unusual behavior of '0 is true, >0 is false' which is
opposite of what is normal in Python, I prefer to explicitly spell it out)
If you instead mean the "&" to simply separate the statements, so that
after the os.system is done, then regardless of the outcome root.destroy
is called-- then... just separate the statements.
You can use them on the same line with a semicolon if you really must.
But don't do that. :-)
Now, all of that said -- I'm not sure what exactly is going WRONG with
your program. You said GUI, and perhaps that's the problem? If you are
calling a unix interactive command line program from within a GUI
context, things are quite likely to go wrong unless you do a lot of
extra work. Are you expecting a new console window to pop up for 'top'
and for that to run on its own? If so -- that won't happen on its own or
with a single function call sort of easy way.
What OS are you on? What are you actually trying to do here?