You are not logged in.

#1 2008-09-24 19:39:34

genisis300
Member
From: Uk
Registered: 2008-01-15
Posts: 284

python sting to a list (solved)

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

#2 2008-09-24 19:52:02

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: python sting to a list (solved)

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

#3 2008-09-24 20:00:24

genisis300
Member
From: Uk
Registered: 2008-01-15
Posts: 284

Re: python sting to a list (solved)

thanks dusty thats worked a treat

thats just an example of a command smile


"is adult entertainment killing our children or is killing our children entertaining adults?" Marilyn Manson

Offline

#4 2008-09-24 20:19:24

genisis300
Member
From: Uk
Registered: 2008-01-15
Posts: 284

Re: python sting to a list (solved)

next question then big_smile

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

#5 2008-09-24 20:28:59

genisis300
Member
From: Uk
Registered: 2008-01-15
Posts: 284

Re: python sting to a list (solved)

hahah worked it out smile


"is adult entertainment killing our children or is killing our children entertaining adults?" Marilyn Manson

Offline

Board footer

Powered by FluxBB