471,052 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,052 software developers and data experts.

HELP: Python equivalent of UNIX command "touch"

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.

-pekka-
Jul 18 '05 #1
5 3144
pekka niiranen <pe************@wlanmail.com> wrote:
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.


You want os.utime()
Jul 18 '05 #2
Roy Smith wrote:
pekka niiranen <pe************@wlanmail.com> wrote:
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.

You want os.utime()


Nope, it does not work for directories in Windows
Jul 18 '05 #3
In article <42**************@wlanmail.com>,
pekka niiranen <pe************@wlanmail.com> wrote:
Roy Smith wrote:
pekka niiranen <pe************@wlanmail.com> wrote:
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.

You want os.utime()


Nope, it does not work for directories in Windows


Well, there's always the old fashioned way (which early versions of
touch used). Read the first byte of the file, rewind, write the byte
back out, seek to the end (to preserve the file length), and close the
file. I'm not sure what to do for directories (I guess you could
create and delete a temp file).

Of course, if you told me that doesn't work on Windows either, I
wouldn't be too surprised. :-)
Jul 18 '05 #4
pekka niiranen <pe************@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+GE NERIC_WRITE,
FILE_SHARE_WRITE,None,OPEN_EXISTING,FILE_FLAG_BACK UP_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
Jul 18 '05 #5
Since no-one's suggested this yet, I highly recommend
UnxUtils: http://unxutils.sourceforge.net/ which includes
a touch.exe. Obviously, this doesn't answer your call for
a Python version, but if you're happy with touch under
Unix, maybe this will work for you.

TJG

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

54 posts views Thread by Brandon J. Van Every | last post: by
reply views Thread by leo | last post: by
36 posts views Thread by Andrea Griffini | last post: by
45 posts views Thread by Gregory Petrosyan | last post: by
5 posts views Thread by Alex Maghen | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.