File attributes

Ben Cartwright bencvt at gmail.com
Tue May 23 00:43:08 CEST 2006


acatejr at gmail.com wrote:
> 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.


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




More information about the Python-list mailing list