File attributes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • acatejr@gmail.com

    #1

    File attributes

    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

    #2
    Re: File attributes

    acatejr@gmail.c om wrote:[color=blue]
    > 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.[/color]


    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:
    Gate is a leading provider of web hosting, domain names, exchange hosting and virtual private servers.


    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 togglefileattri bute(filename, fileattribute, value):
    """Turn a specific file attribute on or off, leaving the other
    attributes intact.
    """
    bitvector = win32file.GetFi leAttributes(fi lename)
    if value:
    bitvector |= fileattribute
    else:
    bitvector &= ~fileattribute
    win32file.SetFi leAttributes(fi lename, bitvector)

    # Sample usage:
    togglefileattri bute('foo.txt', win32con.FILE_A TTRIBUTE_ARCHIV E, True)

    --Ben

    Comment

    • Ben Cartwright

      #3
      Re: File attributes

      Ben Cartwright wrote:[color=blue]
      > acatejr@gmail.c om wrote:[color=green]
      > > 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.[/color]
      >
      >
      > 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 togglefileattri bute(filename, fileattribute, value):
      > """Turn a specific file attribute on or off, leaving the other
      > attributes intact.
      > """
      > bitvector = win32file.GetFi leAttributes(fi lename)
      > if value:
      > bitvector |= fileattribute
      > else:
      > bitvector &= ~fileattribute
      > win32file.SetFi leAttributes(fi lename, bitvector)
      >
      > # Sample usage:
      > togglefileattri bute('foo.txt', win32con.FILE_A TTRIBUTE_ARCHIV E, True)[/color]

      Or to just check the value of the bit:

      def fileattributeis set(filename, fileattr):
      return bool(win32file. GetFileAttribut es(filename) & fileattr)

      print fileattributeis set('foo.txt', win32con.FILE_A TTRIBUTE_ARCHIV E)

      --Ben

      Comment

      • Larry Bates

        #4
        Re: File attributes

        acatejr@gmail.c om wrote:[color=blue]
        > 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.
        >[/color]
        You must have Mark Hammond's win32 package installed, then you can
        (barely tested):

        import win32api
        import win32con

        fattrs=win32api .GetFileAttribu tes(filename)
        if fattrs & win32con.FILE_A TTRIBUTE_ARCHIV E:
        #
        # Archive bit set
        #

        -Larry Bates

        Comment

        Working...