Is it possible to set the date/time of a directory in windows with Python? If so how?

ToddLMorgan ToddLMorgan at
Sat May 13 10:45:53 CEST 2006

I'm trying to set any of the dates (create, modification, access) of a
directory under windows with python.

I'm trying to do this as I'm trying to write a unittest for a directory
cleaning script I'm writing (ie I need the test to set the create/mod
time for some of the directories so that I can be sure that the script
works properly - as it picks dirs based upon their age).

I've tried using the utime( path, times). That doesn't work as the doco
"Whether a directory can be given for path depends on whether the
operating system implements directories as files (for example, Windows
does not)."

So that doesn't work.

So I tried mark hammonds win32 extensions with something like this

import win32file, win32con, pywintypes
filehandle = win32file.CreateFile(file,
None,win32con.OPEN_ALWAYS, 0, None)
win32file.SetFileTime(filehandle, nowWin32, nowWin32, nowWin32)

which works fine for files but fails with "Access Denied" when I invoke
the CreateFile with a directory. This seems to occur no matter the
combination of READ or WRITE I choose for the parameters.

So does anyone have any useful suggestions (other than not using

I've thought of a couple of reallly nasty solutions:
1. Temporarily alter the underlying system clock back to the required
time and create the directory then resetting it back again afterwards.
2. Create the required directory in some form of virtual filesystem -
eg Zip file, rar archive, tar etc. Then extract it to the real
filesystem and hope windows honors the timestamps (untested at this

None of those is particularly appealing.

Thanks for listening.

More information about the Python-list mailing list