how to set the time of a directory?

MRAB google at mrabarnett.plus.com
Mon Oct 13 14:23:00 EDT 2008


On Oct 13, 2:40 pm, oyster <lepto.pyt... at gmail.com> wrote:
> so, let alone the 'bad' os.utime, is there any way to set the time of
> directory in windows within official python release? can ctypes be
> helpful, and how ?
>
This appears to work:

import pywintypes
import win32file
import time

def set_dir_time(path, new_times=None):
    if new_times is None:
        t = time.localtime()
        atime, mtime = t, t
    else:
        atime, mtime = time.localtime(new_times[0]),
time.localtime(new_times[1])
    atime, mtime = pywintypes.Time(atime[0 : 6]),
pywintypes.Time(mtime[0 : 6])
    handle = win32file.CreateFile(path, win32file.GENERIC_WRITE,
win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE, None,
win32file.OPEN_EXISTING, win32file.FILE_FLAG_BACKUP_SEMANTICS, 0)
    win32file.SetFileTime(handle, None, atime, mtime)
    handle.Close()


> I am using totalcommander, which is programmer in delphi, but can set
> the time of a directory
>
> thanx
>
> 2008/10/12, python-list-requ... at python.org <python-list-requ... at python.org>:
>
> > From: MRAB <goo... at mrabarnett.plus.com>
> > To: python-l... at python.org
> > Date: Sat, 11 Oct 2008 11:29:12 -0700 (PDT)
> > Subject: Re: how to set the time of a directory?
> > On Oct 11, 1:18 pm, "drobi... at gmail.com" <drobi... at gmail.com> wrote:
> > > On Oct 11, 1:27 am, "Timothy Grant" <timothy.gr... at gmail.com> wrote:> On Fri, Oct 10, 2008 at 10:16 PM, oyster <lepto.pyt... at gmail.com> wrote:
> > > > > os.utime works only against files. so what to do for a directory?
> > > > > thanx
>
> > > > Not sure why you'd say that.
>
> > > I am. He's running Windows.
>
> > > > drwxr-xr-x  2 tjg  tjg       68 Oct 10 22:23 test
> > > > (tjg at bastard%) python
> > > > Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:16)
> > > > [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
> > > > Type "help", "copyright", "credits" or "license" for more information.>>> import os
> > > > >>> help(os.utime)
>
> > > > Help on built-in function utime in module posix:
>
> > > > utime(...)
> > > >     utime(path, (atime, mtime))
> > > >     utime(path, None)
>
> > > >     Set the access and modified time of the file to the given values.  If the
> > > >     second form is used, set the access and modified times to the current time.
>
> > > > >>> os.utime('test', None)
> > > > >>> ^D
>
> > > > (tjg at bastard%) ls -ltr
> > > > drwxr-xr-x  2 tjg  tjg       68 Oct 10 22:24 test
>
> > > > --
> > > > Stand Fast,
> > > > tjg.  [Timothy Grant]
> > > >>> os.utime('WinDir', None)
>
> > > Traceback (most recent call last):
> > >   File "(stdin)", line 1, in <module>
> > > WindowsError: [Error 5] Access is denied: 'WinDir'
>
> > > I consider this a bug. (Note that os.utime works for directories under
> > > cygwin)
>
> > This in issue #214245 (2000-09-16). See also
> >http://mail.python.org/pipermail/python-bugs-list/2004-November/02612....
>
>




More information about the Python-list mailing list