File attributes

Ben Cartwright bencvt at gmail.com
Tue May 23 00:49:46 CEST 2006


Ben Cartwright wrote:
> 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)

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




More information about the Python-list mailing list