You are not logged in.
Pages: 1
Hi All,
i've been trying to get a string into a list.
i'm running a command and trying to split the output so i can play with it.
import commands, os
s = os.popen("ls").read()
#print s
#print s.split(" ")
for each in s:
menu.append(each.split(" "))
print menu[1]
this is my the latest of my attempts. Does anyone have any ideas?
Thanks
Matthew)
Last edited by genisis300 (2008-09-24 20:29:42)
"is adult entertainment killing our children or is killing our children entertaining adults?" Marilyn Manson
Offline
If you're trying to iterate over the output of ls, you need to split on \n (the newline character):
filenames = s.split('\n')
then you can do:
for filename in filenames:
# do something awesome.
In adition, you don't have to popen ls to get a list of files, take a look at the os.listdir function.
Dusty
Offline
thanks dusty thats worked a treat
thats just an example of a command
"is adult entertainment killing our children or is killing our children entertaining adults?" Marilyn Manson
Offline
next question then
whats the best way to join 2 lists
/usr/bin/python -O
import commands, os
test=[]
test1=[]
temp = commands.getoutput('df | grep / | cut -c57-70')
temp1= commands.getoutput('df | grep / | cut -c51-55')
#print temp
#s = os.popen("ls").read()
#print s
#print s.split(" ")
#for each in s:
# menu.append(each.split(" "))
### get spaces used from df
space = temp1.split('\n')
for spaces in space:
test1.append(spaces)
### get mount points from df
filenames = temp.split('\n')
for filename in filenames:
test.append(filename)
#print test[0]
print test[2] + test1[2] +" Used"
i would like test to be test[number] +test1[corrosponging number]
thanks in advance
Last edited by genisis300 (2008-09-24 20:24:51)
"is adult entertainment killing our children or is killing our children entertaining adults?" Marilyn Manson
Offline
hahah worked it out
"is adult entertainment killing our children or is killing our children entertaining adults?" Marilyn Manson
Offline
Pages: 1