Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > HELP: Python equivalent of UNIX command "touch"

Reply
Thread Tools

HELP: Python equivalent of UNIX command "touch"

 
 
pekka niiranen
Guest
Posts: n/a
 
      02-28-2005
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-
 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      02-28-2005
pekka niiranen <> 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()
 
Reply With Quote
 
 
 
 
pekka niiranen
Guest
Posts: n/a
 
      03-02-2005
Roy Smith wrote:
> pekka niiranen <> 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
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      03-02-2005
In article <>,
pekka niiranen <> wrote:
>Roy Smith wrote:
>> pekka niiranen <> 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.
 
Reply With Quote
 
Wolfgang Strobl
Guest
Posts: n/a
 
      03-02-2005
pekka niiranen <>:

>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
 
Reply With Quote
 
Tim G
Guest
Posts: n/a
 
      03-03-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Equivalent for unix "read" command in rake tasks? Rob Lucas Ruby 9 12-09-2007 07:10 AM
Pure Python equivalent of unix "file" command? W3 Python 2 07-23-2007 12:40 PM
C Program [ Turbo-C ] , to extract only-Printable-characters from a file ( any type of file) and display them ( i.e equivalent to "strings" command in UNIX) SunRise C Programming 7 07-06-2005 08:11 AM
C Program [ Turbo-C ] , to extract only-Printable-characters from a file ( any type of file) and display them ( i.e equivalent to "strings" command in UNIX) SunRise C Programming 2 07-02-2005 11:35 PM
Run Unix shell command $ parse command line arguments in python rkoida@yahoo.com Python 4 04-23-2005 04:42 AM



Advertisments