File attributes

A

acatejr

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.
 
B

Ben Cartwright

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
 
B

Ben Cartwright

Ben said:
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
 
L

Larry Bates

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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,298
Messages
2,571,539
Members
48,274
Latest member
HowardKipp

Latest Threads

Top