Split string

Steve Holden sholden at bellatlantic.net
Thu Feb 10 12:30:17 EST 2000


[posted and mailed]

Pedro Silva wrote:
> 
> Hi,
> 
> I wnat to split a string that has be return by the filesystem
> 
> The code that I'm using is:
> 
>         def obj_fs(self):
>             import os
>             listing=os.listdir("/var/spool/news/articles")
>             str=string.split(listing)
>             return str
> 
> Is this correct?

It isn't terribly obvious what you're trying to do here.  The os.listdir
function returns a list, not a string.  The individual members of that
list can be got at using subscripting: the first item is listing[0], the
next is listing[1], and so on.  So:

>>> import string, os, os.path
>>> listing = os.listdir("R:/ZipDisks/Text Editors")
>>> listing[0]
'emacs-19_34_6-bin-i386_tar.gz'
>>> listing[1]
'pfe0702i.zip'
>>> 

Now, using string.split() on a list won't get you much.  Just an error!

>>> str=string.split(listing)
Traceback (innermost last):
  File "<pyshell#4>", line 1, in ?
    str=string.split(listing)
TypeError: argument 1: expected read-only character buffer, list found
>>> 

It would be better if your obj_fs() function returned the list directly,
although you might want to play with it first and remove things which
aren't of the type you are interested in.  But essentially, the list
will contain the filenames you want - it may contain more, of course, as
Thomas Wouters pointed out in an earlier post.

Does this point you in the right direction?  To mess around with names
of files and directories you may want to look at os.path, which has
functions which take care of many platform-specifics.  Portability is
good!

> 
> Another Problem.
> I'm using the following code in a DTML Method:
> 
>  <a href="msg_subgrupo" target=cima>
>   <dtml-tree branches_expr="objectValues(['Folder',''External Method])"
> assume_children=1>
>    <dtml-var obj_fs>
>   </dtml-tree>
> </a>
> 
>     What I want with this is that, with the tree tag I be able to see the
> subfolders of those folder.
>     Is this correct?

DTML isn't my bag.  You may get replies from Pythonista, but don't
bank on it.  This newsgroup is about Python.  Mostly ... :^)

> 
> Please if you can help me, send your answers to: psilva at ruido-visual.pt
> 
> Thanks,
> 
> Pedro
> 
> PS - I think that now, my text isn't in HTML, or it is?

Seems that all the HTML is gone now!

regards
 Steve
--
"If computing ever stops being fun, I'll stop doing it"



More information about the Python-list mailing list