How to set the Timestamp of a File

Prahlad Vaidyanathan slime at vsnl.net
Fri Mar 22 08:06:53 EST 2002


Hi,

This is my first post to this list, so a "Hi!" to everyone here.

On Fri, 22 Mar 2002 Jörg Schütter spewed into the ether:
> Hi
> 
> How can I set the timestamp of a file (especially under windows) with
> python? In Unix/Linux there is a tool caled touch.
> There is no problem to read the timestamp, but how can I set the
> timestamp to a desired time?

According to the 2.2 docs, os.utime() works. For example, here is a very
simplistic touch :

<code>
import os, time

def touch (filename) :
    new_time = time.time()
    try :
        os.utime(filename, (new_time, new_time))
    except OSError :
        # Create the file, if it doesn't exist
        fd = open(filename,'a')
        fd.close()

if __name__ == "__main__" :
    touch(sys.argv[1])
</code>

HTH, 
pv.
-- 
Prahlad Vaidyanathan  <http://www.symonds.net/~prahladv/>

I drink to make other people interesting.
		-- George Jean Nathan






More information about the Python-list mailing list