You are not logged in.

#1 2007-01-23 08:03:33

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

python os.system

I am trying to get args passed in bash to

$1 etc ...

wondered if I can fire a system command from python?

errr

import os

os.system('free')

wondered if popen was a better option

ok ok I am looking honest lol


Mr Green

Offline

#2 2007-01-23 09:19:30

arkanoid
Member
Registered: 2006-08-08
Posts: 25

Re: python os.system

hey mister green,

take a look at the commands module.

small example :

import commands

program = "ls"
arg = commands.mkarg("-all")   # usefull adding space ahead 

output = commands.getoutput(program + arg)

Offline

#3 2007-01-23 11:04:27

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: python os.system

os.popen is for piping.

I prefer subprocess personally. I think they're pushing people to use it over command() os.system() os.spawnvp() etc, etc. There is also many conversion examples in the help.

from subprocess import Popen

retcode = Popen('mr green shell command', shell=True).wait()
if retcode != 0:
    print 'mr greens program failed. Damninations.'

Offline

#4 2007-01-23 11:24:28

arkanoid
Member
Registered: 2006-08-08
Posts: 25

Re: python os.system

does Subprocess fork() the current process like the name suggests ?

Depends maybe on the usage, the reason I like commands.getouput is that it's a very convenient way to retrieve the result of a command in a list of lines. I'm not sure popen does the same

Offline

#5 2007-01-23 14:23:14

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: python os.system

I have heard of popen ... but not Popen

Emmm looks like what I need fire a bash command then exit...

managed to get 3 scripts down to 2 but I have been calling a bash script from python messy.....

see how it goes thanks


Mr Green

Offline

#6 2007-01-23 16:15:07

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: python os.system

Technically you should use subprocess in place of popen - it is supposed to be the most modern one

Offline

#7 2007-01-23 16:19:19

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: python os.system

well its just that I had never heard of it. .. so I'm going to give it go....

works fine... only I cannot use it in openbox menu

execute calls command ... so ... emm will have think

ie when you click/select item in menu I want command to run


Mr Green

Offline

#8 2007-01-24 10:58:08

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: python os.system

right I have managed to get three scripts down to two ...

What I need is to have python script pipe output of bash commands to /tmp/cmd

confused about if I should use popen or os.system ... writing to file should not bee a problem just need to get info

/me thinks


Mr Green

Offline

#9 2007-01-24 11:31:08

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: python os.system

def foo(x, f):
    x = open(x)
    l = x.write()
    x.close()
    return l

not sure about this need to get output from command into /tmp/cmd

reading command I use os.popen

getting in over my head lol


Mr Green

Offline

Board footer

Powered by FluxBB