HELP: Python equivalent of UNIX command "touch"

Wolfgang Strobl news2 at mystrobl.de
Wed Mar 2 15:58:48 EST 2005


pekka niiranen <pekka.niiranen at wlanmail.com>:

>Does anybody know Python recipe for changing the date
>of the directory or files in W2K to current date and time?
>In UNIX shell command "touch" does it.

See below. The key is using the FILE_FLAG_BACKUP_SEMANTICS flag.

#----------------------------------------------------------------------------------
# dirtest.py
from win32file import *
from pywintypes import Time
import time
x=CreateFile(r"d:\scratch\testdir",GENERIC_READ+GENERIC_WRITE,
    FILE_SHARE_WRITE,None,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,0)
i,c,a,w= GetFileTime(x)
print "create",c,"access",a,"write",w
SetFileTime(x,Time(int(c)-24*3600),Time(int(c)-12*3600),Time(int(c) 
    -3*3600))
#----------------------------------------------------------------------------------

C:\e\littlepython>dirtest.py
create 01/21/05 04:27:04 access 01/21/05 16:27:04 write 01/22/05
01:27:04

C:\e\littlepython>dirtest.py
create 01/20/05 03:27:04 access 01/20/05 15:27:04 write 01/21/05
00:27:04

C:\e\littlepython>dir d:\scratch\testdir
...
 Verzeichnis von d:\scratch\testdir

20.01.2005  00:27       <DIR>          .
20.01.2005  00:27       <DIR>          ..
               0 Datei(en)              0 Bytes
               2 Verzeichnis(se),     806.768.640 Bytes frei

-- 
Wir danken für die Beachtung aller Sicherheitsbestimmungen



More information about the Python-list mailing list