Implementing some of 'ls -l" directly in python
Carl Banks
imbosol at vt.edu
Tue Nov 19 19:32:05 EST 2002
Tim Daneliuk wrote:
> I have been looking high and low for several hours to answer a (probably
> simple) question with no luck. So, to the PyGeniuses I go:
>
> I want to implement something similar to an 'ls -l file' in a script I'm
> writing. I'd like to avoid actually spawning 'ls' because I want to
> script to run on Win32 as well as *nix.
>
> So, I use os.path.getmtime and os.path.getsize for two of the things I
> need.
Why? These are also returned by os.stat. I'd guess that these do a
stat internally to get the information, meaning that, for every file,
your program does three stats. I recommend using only one call to
os.stat.
(And, here on Linux, stat seems to be the most annoyingly slow system
call, so I'd never do more than I had to.)
> I then stat the file to get its UID, GID, and permission bits.
> Here's where I get stuck. I cannot seem to find a portable way to
> convert the UID/GID into their equivalent string names.
For unix, see the pwd and grp modules. No clue how to do it for
Windows, and I doubt you can do it portably. (Does Windows even use
user ids? or just the names?) Filesystems are just too different to
really do anything completely portably; you can only depend on the
Python standard library to do so much.
--
CARL BANKS
More information about the Python-list
mailing list