HELP: Python equivalent of UNIX command "touch"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pekka niiranen

    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-
  • Roy Smith

    #2
    Re: HELP: Python equivalent of UNIX command "touch&quo t;

    pekka niiranen <pekka.niiranen @wlanmail.com> wrote:[color=blue]
    >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.[/color]

    You want os.utime()

    Comment

    • pekka niiranen

      #3
      Re: HELP: Python equivalent of UNIX command &quot;touch&quo t;

      Roy Smith wrote:[color=blue]
      > pekka niiranen <pekka.niiranen @wlanmail.com> wrote:
      >[color=green]
      >>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.[/color]
      >
      >
      > You want os.utime()[/color]

      Nope, it does not work for directories in Windows

      Comment

      • Roy Smith

        #4
        Re: HELP: Python equivalent of UNIX command &quot;touch&quo t;

        In article <4225E4E9.50709 03@wlanmail.com >,
        pekka niiranen <pekka.niiranen @wlanmail.com> wrote:[color=blue]
        >Roy Smith wrote:[color=green]
        >> pekka niiranen <pekka.niiranen @wlanmail.com> wrote:
        >>[color=darkred]
        >>>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.[/color]
        >>
        >>
        >> You want os.utime()[/color]
        >
        >Nope, it does not work for directories in Windows[/color]

        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. :-)

        Comment

        • Wolfgang Strobl

          #5
          Re: HELP: Python equivalent of UNIX command &quot;touch&quo t;

          pekka niiranen <pekka.niiranen @wlanmail.com>:
          [color=blue]
          >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.[/color]

          See below. The key is using the FILE_FLAG_BACKU P_SEMANTICS flag.

          #----------------------------------------------------------------------------------
          # dirtest.py
          from win32file import *
          from pywintypes import Time
          import time
          x=CreateFile(r" d:\scratch\test dir",GENERIC_RE AD+GENERIC_WRIT E,
          FILE_SHARE_WRIT E,None,OPEN_EXI STING,FILE_FLAG _BACKUP_SEMANTI CS,0)
          i,c,a,w= GetFileTime(x)
          print "create",c,"acc ess",a,"write", w
          SetFileTime(x,T ime(int(c)-24*3600),Time(i nt(c)-12*3600),Time(i nt(c)
          -3*3600))
          #----------------------------------------------------------------------------------

          C:\e\littlepyth on>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\littlepyth on>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\littlepyth on>dir d:\scratch\test dir
          ....
          Verzeichnis von d:\scratch\test dir

          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 Sicherheitsbest immungen

          Comment

          • Tim G

            #6
            Re: HELP: Python equivalent of UNIX command &quot;touch&quo t;

            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

            Comment

            Working...