import os


11 posts
by Hiro » Wed Aug 08, 2012 7:06 pm
I need to run a command line function from a python script, on my main rig i do this:
Code: Select all
import os
os.system ("nbtscan -e")

This works fine and i get the output i expect but on my Pi i get the output" 512". All other shell commands also just give a number as the reply. Is the os module not installed? I did google but was unable to figure out how to install modules or even figure out what is actually installed. Any ideas?
Cheers Hiro.

PS I am using the latest raspbian (not updated though) and have tried 2.7 and 3.2 versions of python.
Posts: 75
Joined: Fri Dec 30, 2011 11:09 am
by jackokring » Wed Aug 08, 2012 7:20 pm
Sounds like a return code is being produced. Is perhaps your regular PC a windows PC? If so then the output from system is going nowhere, and needs redirecting.

try

nbtscan > /dev/tty1

It may work, but maybe not, I have no test here.

Cheers Jacko
Pi=B256R0USB CL4SD8GB Raspbian Stock. https://sites.google.com/site/rubikcompression/strictly-long
User avatar
Posts: 751
Joined: Tue Jul 31, 2012 8:27 am
Location: London, UK
by BlackJack » Wed Aug 08, 2012 7:26 pm
@Hiro: The `os.system()` function *returns* the exit code of the command (or the shell that is used to execute the given command). That is the number you see when you execute the function in a Python shell or if you ``print`` the return value.

Code: Select all
In [7]: os.system('ls python_games/*.py')
python_games/blankpygame.py              python_games/pentomino.py
python_games/catanimation.py             python_games/simulate.py
python_games/drawing.py                  python_games/slidepuzzle.py
python_games/flippy.py                   python_games/squirrel.py
python_games/fourinarow.py               python_games/starpusher.py
python_games/gemgem.py                   python_games/tetrominoforidiots.py
python_games/inkspill.py                 python_games/tetromino.py
python_games/memorypuzzle_obfuscated.py  python_games/wormy.py
python_games/memorypuzzle.py
Out[7]: 0


An exit code of 0 usually means the program ran fine.
Code: Select all
while not self.asleep():
    sheep += 1
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
by Hiro » Wed Aug 08, 2012 8:01 pm
My other computer runs Mint Linux and Python 2.7.2+, the text shown in the first post returns the correct output followed by 0 whist on the Pi it only shows the 0 (sorry the earlier 512 was erroneous), why the difference. I'm typing in the interpreter on both and the Pi runs version 2.7.3rc2.
By the way i short typed the code earlier before that confuses anyone. the actual code is.
Code: Select all
os.system ("nbtscan -e 192.168.1.1-100")

If however i type:
Code: Select all
os.system ("nbtscan -e 192.168.1.1-100") > /home/pi/nbt.txt

Then i get the text file with the correct info inside, but still can't understand why the difference between the two nearly identical python versions. How do i get the interpreter to show the output on the Pi like it does on the Mint box?
To be honest this is the first time i have used the interpreter, i usually use Geany and that is probably what i shall go back to, this was just a quick way to test things out. Typing the same command into Geany on the Pi and running from there opened up a terminal and showed the results as it should.
I guess the problem all along is just my lack of experience with the interpreter.
Cheers, Hiro.
PS Is the interpreter worth persevering with for any reason or should i go back to Geany?
Posts: 75
Joined: Fri Dec 30, 2011 11:09 am
by BlackJack » Wed Aug 08, 2012 9:34 pm
@Hiro: Is it possible that by (Python) interpreter on the Raspi you actually mean *Idle*!? Then the output of external processes goes to the stdout of Idle and not into the GUI window of the Python shell. You may find the output in the file ``~/.xsession-errors``.

If you want to see „console output” of external programs than run the Python program from a console.
Code: Select all
while not self.asleep():
    sheep += 1
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
by Hiro » Thu Aug 09, 2012 4:19 pm
I'm just curious as to why one version outputs to a terminal it opens and the other doesn't. When i run the command on my Mint machine a terminal pops up and displays the information but on the Pi it doesn't. Both version 2.7 but i suppose it could be just a config issue. As i say it doesn't matter as i've gone back to using Geany instead of, as you say IDLE, i called it the interpreter as that is what a lot of Python tutorial websites seem to call it. I must admit i don't find IDLE particularly natural to use, the last programming i did was in basic (yes i'm that old) so i suppose i'm tied to that line numbering system that it used and i get the same in Geany.
Cheers, Hiro.
Posts: 75
Joined: Fri Dec 30, 2011 11:09 am
by BlackJack » Thu Aug 09, 2012 7:13 pm
I'm still not clear what you are actually running where. The Python interpreter, an executable called `python` does not open a terminal. At least not on Linux. Never. Windows is a different beast because there is a difference between console and GUI application EXE files and when you start a console application the operating system opens a console for that program by default (even if you don't want that). IDLE is a development environment which embeds an interactive interpreter in a window. It shows text output from Python in that window by „highjacking” `sys.stdout`. That is where all text output via ``print`` comes by. But it does not know about text output of external programs. That's why you don't see it in that window.
Code: Select all
while not self.asleep():
    sheep += 1
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
by lars12 » Thu Aug 09, 2012 7:34 pm
Hi Hiro,

I agree with BlackJack - you need to run the bare metal ("python") if you want to compare the behaviour on different platforms. And I have to admit that the pure default python interpreter is perfectly fine for all my tasks.

Additionally I would recommend you to try the following code instead:

Code: Select all
import subprocess
proc = subprocess.Popen(["ls", "-la", "/tmp/"],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)
(output, errors) = proc.communicate()
if proc.returncode != 0:
    print "Failed to execute command: %s" % errors
else:
    print output

(the exception handling is left as an exercise for you ...)

The "os.system" function is obsolete - you should consider it to be a dirty hack that you should avoid whenever you do something serious.
But anyway: don't trust strangers like me - you should better read the following bits instead:

In general: I would recommend you to get used to python's internal help. It is fabulous and you will probably like the feeling of being empowered again :)
Code: Select all
import subprocess
dir(subprocess)
help(subprocess)
help(subprocess.Popen)


Have fun!
Posts: 4
Joined: Thu Aug 09, 2012 12:41 pm
by Hiro » Thu Aug 09, 2012 8:31 pm
It would appear the confusion is my end, just for a change:). Clicking on the Python icon on the Pi brought up IDLE, clicking on the Python icon on Mint brought up a terminal with exactly the same text in as IDLE. So i thought they were the same thing but just noticed the Pi window is titled Python Shell and the Mint window is titled Terminal. I have now installed IDLE on Mint and normality has been resumed.
@blackjack. The desktop icon on Mint points to /usr/bin/python2.7 and this does open up a terminal with as i said above exactly the same code as IDLE starts with but it is not IDLE it is a Gnome Terminal (mint remember) is this wrong? I suppose it doesn't really matter now but i was just interested in why it was doing it if you say it shouldn't. Or i am confused (yet again) and this /usr/bin/python2.7 isn't the interpreter? Sorry for all the questions but confusion has well and truly set in.

@lars12. I picked up a magazine from the newsagent yesterday and by coincidence when i got home it had a basic Python tutorial in it which covered os.system commands. Now i find out it's out of date, doesn't the world of computing move fast these days.
Seriously thanks for the info, i am very much at the bottom of a steep learning curve that seems vertical sometimes. I have had a look at your links and think i can work it out and i will definitely check out the Python help.
Thanks for all your help, Hiro.
Posts: 75
Joined: Fri Dec 30, 2011 11:09 am
by BlackJack » Thu Aug 09, 2012 10:55 pm
@Hiro: ``/usr/bin/python2.7`` is the Python interpreter but it does not open a terminal. It does not have an icon either. The icon is part of your desktop environment and that also opens the terminal when you click on the icon.
Code: Select all
while not self.asleep():
    sheep += 1
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
by Hiro » Fri Aug 10, 2012 3:51 pm
See, i told you it was me that was confused, thanks for clearing that up Blackjack.
Posts: 75
Joined: Fri Dec 30, 2011 11:09 am