Best way to 'touch' a file?
Fredrik Lundh
fredrik at pythonware.com
Mon Aug 22 18:46:24 EDT 2005
Peter Hansen wrote:
> And, depending on your needs, quite ineffective:
>
> >>> import os
> >>> os.utime('missing.file', None)
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> OSError: [Errno 2] No such file or directory: 'missing.file'
>
> >>> from path import path
> >>> path('missing.file').touch()
> >>> path('missing.file').exists()
> True
>
> I guess it depends on whether "touch" implies creation-when-missing, as
> with the command line version, or just updating the time.
the OP wanted "to update the modification time of a file without actually
modifying it". os.utime does exactly that; no more, no less, and no extra
dependencies.
</F>
More information about the Python-list
mailing list