HELP: Python equivalent of UNIX command "touch"

Discussion in 'Python' started by pekka niiranen, Feb 28, 2005.

  1. 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-
     
    pekka niiranen, Feb 28, 2005
    #1
    1. Advertising

  2. pekka niiranen

    Roy Smith Guest

    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()
     
    Roy Smith, Feb 28, 2005
    #2
    1. Advertising

  3. 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
     
    pekka niiranen, Mar 2, 2005
    #3
  4. pekka niiranen

    Roy Smith Guest

    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. :)
     
    Roy Smith, Mar 2, 2005
    #4
  5. 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+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
     
    Wolfgang Strobl, Mar 2, 2005
    #5
  6. pekka niiranen

    Tim G Guest

    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
     
    Tim G, Mar 3, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    1,774
    Terry Hancock
    Apr 23, 2005
  2. W3
    Replies:
    2
    Views:
    771
    c james
    Jul 23, 2007
  3. Han Holl
    Replies:
    4
    Views:
    368
    Nobuyoshi Nakada
    Oct 12, 2006
  4. Rob Lucas
    Replies:
    9
    Views:
    176
    Thufir
    Dec 9, 2007
  5. Melroy

    Perl equivalent of unix time command

    Melroy, Sep 7, 2008, in forum: Perl Misc
    Replies:
    13
    Views:
    221
Loading...

Share This Page