Open script

Thomas Wouters thomas at xs4all.net
Thu Feb 10 09:06:11 EST 2000


On Thu, Feb 10, 2000 at 12:53:38PM -0000, Pedro Silva wrote:

> I'm trying to use OPEN to open a folder, because I need to get the content
> of that folder.
> This folder is in the filesystem.
> The code line that I'm using is:
>             f = open('var/spool/news/articles','r')
>             fs = f.realines()
>             return fs
> But this is giving me and error. Is this correct or because articles is a
> folder and is because of that that the error is displayed.
> Please send me your answers to: psilva at ruido-visual.pt

While Long Ago in the UNIX world, folders (directories) were just another
kind of file, and you had to read them manually, this has been abandoned
long ago ;) As far as I know, all operating systems nowadays use
opendir/readdir-style interfaces to folders (directories). Python has (of
course) a friendly little interface to that: os.listdir():

>>> import os
>>> os.listdir("tst")
['tmp']

Note that os.listdir() omits the . and .. directories, which are 'normal'
directories under unix, but special magic thingies under win32 (IIRC,
anyway) and dont actually exist under Mac (again, IIRC.) You'll have to add
them manually if you do expect them to be there.

Two notes though: if you do a lot of listdir()s, check out the dircache
module. And if you plan to be portable, dont use slashes in paths, use
os.path.join() on the seperate segments. But then, the above path is fairly
platform dependant ;)

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list