[Python-Dev] Telling directories from files (Was: statcache.lstat?)

Martin von Loewis loewis@informatik.hu-berlin.de
Thu, 1 Nov 2001 14:45:18 +0100 (MET)


> File selector goodies peek at all files in a directory, even the
> stuff you don't care about, at the very least to segregate them into
> directory and non-directory files.  Caching stat info would probably
> help speed them up.  I was trying to speed things up a bit and saw
> statcache.

Some of the recent systems have the file type in the directories
(44ffs, NTFS, ext2 with "filetype" feature); in addition, recent C
libraries expose the file type through getdents. E.g. on Linux,
struct dirent is

struct dirent64
  {
    __ino64_t d_ino;
    __off64_t d_off;
    unsigned short int d_reclen;
    unsigned char d_type;
    char d_name[256];
  };

In this type, d_type can take the values

DT_UNKNOWN
DT_FIFO
DT_CHR
DT_DIR
DT_BLK
DT_REG
DT_LNK
DT_SOCK
DT_WHT

Likewise, on Win32, FindFirstFile will return not only the file name,
but also whether it is a directory, access times, alternate names,
etc.

I always meant to expose the file type to Python, to speed up things
that only do stat to tell apart directories from non-directories. Of
course, doing this in a portable, backwards-compatible manner may
become a challenge, especially if you want os.listdir to expose this
information.

I think I'll write a PEP.

Regards,
Martin