[Python-Dev] Returning Windows file attribute information via os.stat() (original) (raw)

Ben Hoyt benhoyt at gmail.com
Tue Jun 10 14:19:54 CEST 2014


> FILEATTRIBUTEHIDDEN = 2 # constant defined in Windows.h > > if hasattr(st, 'stwinattrs') and st.stwinattrs & FILEATTRIBUTEHIDDEN:

I don't like such API, it requires to import constants, use masks, etc. I would prefer something like: if st.winhidden: ... Or maybe: if st.winattrs.hidden: ...

Yes, fair call. However, it looks like the precent for the attributes in os.stat()'s return value has long since been set -- this is OS-specific stuff. For example, what's in "st_flags"? It's not documented, but comes straight from the OS. Same with st_rdev, st_type, etc -- the documentation doesn't define them, and it looks like they're OS-specific values.

I don't think the st.win_hidden approach gains us much, because the next person is going to ask for the FILE_ATTRIBUTE_ENCRYPTED or FILE_ATTRIBUTE_COMPRESSED flag. So we really need all the bits or nothing. I don't mind the st.st_winattrs.hidden approach, except that we'd need 17 sub-attributes, and they'd all have to be documented. And if Windows added another attribute, Python wouldn't have it, etc. So I think the OS-defined constant is the way to go.

Because these are fixed-forever constants, I suspect in library code and the like people would just KISS and use an integer literal and a comment, avoiding the import/constant thing:

if getattr(st, 'st_winattrs', 0) & 2:  # FILE_ATTRIBUTE_HIDDEN
    ...

-Ben



More information about the Python-Dev mailing list