PDA

View Full Version : Execute terminal commands in python



rob1101
March 19th, 2008, 09:39 PM
Sorry for the simple question, but how can i run terminal commands in python?

LaRoza
March 19th, 2008, 09:40 PM
import os
os.system("echo 'hello world'")

rob1101
March 19th, 2008, 09:52 PM
thanks for the extremely fast reply but i have 1 more question (hopefully :P )

how would I use variables with that?

example



import os

x = "hello world"

os.system("echo x")


EDIT: On a different note is it possible to run this through the IDLE IDE? or do i have to run the script through the terminal?

LaRoza
March 19th, 2008, 09:59 PM
thanks for the extremely fast reply but i have 1 more question (hopefully :P )

how would I use variables with that?


system() takes a string, so any string can be passed.



echo = "echo"
helloWorld = "Hello world"
os.system(echo + " " + helloworld)


It does run in IDLE, but I didn't see the results as IDLE isn't a terminal.

LaRoza
March 19th, 2008, 10:07 PM
You can also use popen: http://docs.python.org/lib/module-popen2.html

bescritt
March 19th, 2008, 10:17 PM
To get exit status you can do the following:


import commands
onAC_Power=True if comands.getstatus("on_ac_power")==0 else False

rob1101
March 20th, 2008, 05:36 AM
thanks for all the help guys it was just what i needed.

another question though. How would i put all of my functions into a seprate file and include them in the main program?

LaRoza
March 20th, 2008, 05:38 AM
thanks for all the help guys it was just what i needed.

another question though. How would i put all of my functions into a seprate file and include them in the main program?

Put them in the file, put this at the top of the file that uses them:



import filename
#Do not type the extension!

Quikee
March 20th, 2008, 07:29 AM
To get exit status you can do the following:


import commands
onAC_Power=True if comands.getstatus("on_ac_power")==0 else False

umm.. minor detail but it is just enough to say:

onAC_Power=comands.getstatus("on_ac_power") == 0

rob1101
March 20th, 2008, 03:58 PM
Put them in the file, put this at the top of the file that uses them:



import filename
#Do not type the extension!


ok thanks, i had it right but didn't know about not putting the file extension. Thats why it didn't work.

thanks everyone for the help.

harakiri1976
April 9th, 2008, 04:38 AM
Hi guys!


How can I get autocompletion in bash command in Python?
What I am saying is, imagine you have a script in Python and you want to select a file called file123.avi in your current working directory but you want to type it just the initial characters and TAB it just like you do it in the shell. I am trying to do that and I can't see how! :(

For instance:


import os

a = str(raw_input('Choose the file: '))
b = os.getcwd()



...in a I wanted to write just 2 or 3 characters of the file and then "TAB" it. Can you help me?
I also tried to import bash this way...



import os

a = os.system('bash')


But then I can't "TAB" it to chose the file :(

pmasiar
April 9th, 2008, 01:21 PM
I don't think it is possible. And you are much better off starting own thread instead of hijacking existing one, likely unrelated to your question.

Also, you imported os, not bash -- you tried to execute bash, likely to no success?

bescritt
April 9th, 2008, 05:19 PM
It is possible to enable completion.
Look at the readline module.

Use

import readline
and now input will use completion.

Not sure whether it supports filename completion though.

harakiri1976
April 10th, 2008, 01:07 AM
bestcritt....

Thank you! But readline works for completion just in Python commands. Thank you anyway!


pmasiar...


If you read back, you can see I imported os but I also "called" bash. And Python did what was supposed to do. Which was, put me in bash. If I press CTRL+D I am again in Python prompt.
Thanks anyway

pmasiar
April 10th, 2008, 02:34 AM
I was under impression that what you want is to have bash-like filename completion in input of raw_input(). So starting bash is not a success, right?

It is hard to guess what is your level of expertize and on what level I should answer. I probably should just say nothing, as I did not have time to do deeper research. OK, next time I will try to ignore more questions and say less. Easier life for me :-)

harakiri1976
April 10th, 2008, 04:30 AM
Yes, that's right! :) I wanted filename completion.
..."start bash" wasn't a success.
No, no...any help is HELP. Thank You anyway! ;)

It's just a script to convert .FLV video to .AVI using mencoder commands. So it runs in the current working directory but the Input file (.flv Video) must be typed by hand. I was just trying to complete it with bash-like behavior. Can you see the point? :)

It works well though.
I wrote the instructions in Portuguese. I'll translate it later and post the code here...

Thanks!

nanotube
April 10th, 2008, 06:46 AM
about executing commands, you really should use the subprocess module, which replaces os.system, popen2, commands, and all related friends. see tho documentation for it here:
http://docs.python.org/lib/module-subprocess.html

N369
June 29th, 2008, 09:38 PM
Suppose I wanted to have a list variable that contains all the files in a given directory...

In Perl, this would be:
@files = `ls /home/user/`;

and then I could use a subscript on the array @files to access each file.

How can I do this in python?

nanotube
June 29th, 2008, 11:21 PM
Suppose I wanted to have a list variable that contains all the files in a given directory...

In Perl, this would be:
@files = `ls /home/user/`;

and then I could use a subscript on the array @files to access each file.

How can I do this in python?


import os
fileslist = os.listdir('/home/user')

in general, you can look around in the documentation for os and os.path modules for a number of ways to do it. (http://www.python.org/doc)

eng_akyq
May 15th, 2009, 05:02 PM
Dear Forum Expert
I need your help to insert a terminal command in Python for the following Python Script:

import bluetooth

print "performing inquiry..."

nearby_devices = bluetooth.discover_devices(lookup_names = True)

print "found %d devices" % len(nearby_devices)

for name, addr in nearby_devices:
print " %s - %s" % (addr, name)

I need after finding the bluetooth devices, the command sending a text file to them by Obexftp terminal command.

Thank you very much

ibuclaw
May 15th, 2009, 05:51 PM
eng_akyq, you will be better off creating a thread about your problem, rather than posting in an old one that hasn't had activity in 11 months, which is considered rather taboo here. If you wish to further pursue asking your question, please do so clicking here:
http://ubuntuforums.org/newthread.php?do=newthread&f=39
Which will create a new thread in this forum.

Due to (now) being offtopic to the original thread question, and being inactive for so long, this thread has been closed.

Regards
Iain