Best way to 'touch' a file?

Peter Hansen peter at engcorp.com
Sun Aug 21 19:26:16 EDT 2005


Kenneth McDonald wrote:
> I could've sworn python had such a command, but now I can't find it...
> 
> I'm looking for an easy way to perform a UNIX-style "touch", to  update 
> the modification time of a file without actually modifying it.  I could 
> do something (I imagine) like opening the file for appending  and then 
> immediately closing it, but that doesn't seem like a good  idea--what if 
> the file is already open for reading or writing? Anyone  know of a nice, 
> elegant solution?

from path import path
path('myfile').touch()

(That relies on Jason Orendorff's path.py module, which does this and 
much more very elegantly, not to mention practically.)

-Peter



More information about the Python-list mailing list