Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

File attributes

54 views
Skip to first unread message

aca...@gmail.com

unread,
May 22, 2006, 6:17:19 PM5/22/06
to
I know how to "walk" a folder/directory using Python, but I'd like to
check the archive bit for each file. Can anyone make suggestions on
how I might do this? Thanks.

Ben Cartwright

unread,
May 22, 2006, 6:43:08 PM5/22/06
to


Since the archive bit is Windows-specific, your first place to check is
Mark Hammond's Python for Windows Extensions (aka win32all). It's a
quick and painless install; grab it here:
http://python.net/crew/skippy/win32/

Once you have that installed, look in the PyWin32.chm help file for the
function calls you need. If the documentation is too sparse, check
MSDN or google it.

For what you're trying to do:

import win32file
import win32con

def togglefileattribute(filename, fileattribute, value):
"""Turn a specific file attribute on or off, leaving the other
attributes intact.
"""
bitvector = win32file.GetFileAttributes(filename)
if value:
bitvector |= fileattribute
else:
bitvector &= ~fileattribute
win32file.SetFileAttributes(filename, bitvector)

# Sample usage:
togglefileattribute('foo.txt', win32con.FILE_ATTRIBUTE_ARCHIVE, True)

--Ben

Ben Cartwright

unread,
May 22, 2006, 6:49:46 PM5/22/06
to

Or to just check the value of the bit:

def fileattributeisset(filename, fileattr):
return bool(win32file.GetFileAttributes(filename) & fileattr)

print fileattributeisset('foo.txt', win32con.FILE_ATTRIBUTE_ARCHIVE)

--Ben

Larry Bates

unread,
May 22, 2006, 6:50:18 PM5/22/06
to aca...@gmail.com
You must have Mark Hammond's win32 package installed, then you can
(barely tested):

import win32api
import win32con

fattrs=win32api.GetFileAttributes(filename)
if fattrs & win32con.FILE_ATTRIBUTE_ARCHIVE:
#
# Archive bit set
#

-Larry Bates

0 new messages