Best way to 'touch' a file?
9 answers - 710 bytes -

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 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 's path.py module, which does this and
much more very elegantly, not to mention practically.)
-Peter
No.1 | | 484 bytes |
| 
Peter Hansen wrote:
from path import path
path('myfile').touch()
import os
os.utime('myfile', None)
is a bit shorter, of course.
help(os.utime)
Help on built-in function utime:
utime()
utime(path, (atime, utime))
utime(path, None)
Set the access and modified time of the file to the given values. If
the
second form is used, set the access and modified times to the current
time.
</F
No.2 | | 808 bytes |
| 
Fredrik Lundh wrote:
Peter Hansen wrote:
>>from path import path
>>path('myfile').touch()
import os
os.utime('myfile', None)
is a bit shorter, of course.
And, depending on your needs, quite ineffective:
import os
os.utime('missing.file', None)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
SError: [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.
-Peter
No.3 | | 735 bytes |
| 
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 ?
SError: [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 P 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
No.4 | | 868 bytes |
| 
Fredrik Lundh wrote:
Peter Hansen wrote:
>>I guess it depends on whether "touch" implies creation-when-missing, as
>>with the command line version, or just updating the time.
the P 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.
You've quoted selectively. He also said "Unix-style 'touch'", from
which one could quite legitimately infer he wants the other features of
the Unix touch command, including the automatic creation of missing files.
Unless you know something more about the P's needs than he's posted
publicly, you're just guessing too even if we both agree yours is the
more likely interpretation.
-Peter
No.5 | | 1133 bytes |
| 
Peter Hansen wrote:
Fredrik Lundh wrote:
>>Peter Hansen wrote:
>>
I guess it depends on whether "touch" implies creation-when-missing, as
with the command line version, or just updating the time.
>>
>>the P 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.
You've quoted selectively. He also said "Unix-style 'touch'", from
which one could quite legitimately infer he wants the other features of
the Unix touch command, including the automatic creation of missing files.
Unless you know something more about the P's needs than he's posted
publicly, you're just guessing too even if we both agree yours is the
more likely interpretation.
Which we probably all can. It's a right bugger when you actually have to
listen to what the customer wants, innit? ;-)
regards
Steve
No.6 | | 204 bytes |
| 
Peter Hansen wrote:
You've quoted selectively. He also said "Unix-style 'touch'", from
which one could quite legitimately infer
nope. read his post again.
</F>
No.7 | | 868 bytes |
| 
Fredrik Lundh wrote:
Peter Hansen wrote:
>>You've quoted selectively. He also said "Unix-style 'touch'", from
>>which one could quite legitimately infer
nope. read his post again.
Sigh. You're being tiring, Fredrik:
'''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.'''
And if your point is that I spelled UNIX in mixed case, and change the
double quotation marks to single quotation marks, you really need to
take a break.
If your point is that this statement *clearly and unambiguously* rejects
the create-if-missing feature as undesirable, then I can say only that
you are simply wrong.
-Peter
No.8 | | 1771 bytes |
| 
Peter Hansen wrote:
Fredrik Lundh wrote:
>>Peter Hansen wrote:
>>
>>
You've quoted selectively. He also said "Unix-style 'touch'", from
which one could quite legitimately infer
>>
>>nope. read his post again.
Sigh. You're being tiring, Fredrik:
You probably mean "tiresome". Bots can be like that sometimes. And not
only bots
'''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.'''
And if your point is that I spelled UNIX in mixed case, and change the
double quotation marks to single quotation marks, you really need to
take a break.
If your point is that this statement *clearly and unambiguously* rejects
the create-if-missing feature as undesirable, then I can say only that
you are simply wrong.
I rather suspect his point is that the P's problem description
specifically implies the file's prior existence. As I believe Fredrik
did, I read "update the modification time of a file" to mean that the
file already has a modification time. This would make the import of the
path module you mentioned a little over the top given there's already a
function in os to handle the requirement.
Given that both solutions have been presented, as far as the rest of the
list is concerned we are probably all three just being tiresome now. The
P can choose whichever best meets his real requirements, whether they
were accurately stated or not.
regards
Steve
No.9 | | 1823 bytes |
| 
Tue, 23 Aug 2005 08:23:40 -0400, Peter Hansen <peter (AT) engcorp (DOT) comwrote:
>Fredrik Lundh wrote:
>Peter Hansen wrote:
>
You've quoted selectively. He also said "Unix-style 'touch'", from
which one could quite legitimately infer
>
>nope. read his post again.
>
>Sigh. You're being tiring, Fredrik:
>
>'''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.'''
>
>And if your point is that I spelled UNIX in mixed case, and change the
>double quotation marks to single quotation marks, you really need to
>take a break.
>
>If your point is that this statement *clearly and unambiguously* rejects
>the create-if-missing feature as undesirable, then I can say only that
>you are simply wrong.
>
TH, if I had to bet, I would bet that the part where it says, " to
update the modification time of a file" indicates a pre-existing file.
it wouldn't be "updating," but just creating a first time-stamp
along with the new file ;-)
At least, that's what I imagine when I see those words. In your favor, I
think the comma after '"touch"' tends to disconnect the purpose that follows,
perhaps even from consciousness, reading quickly. What if the emphasis were changed?
E.g., how would you have interpreted the same words rearranged as follows?
'''I'm looking for an easy way to update the modification time of a file
(to perform a UNIX-style "touch") without actually modifying it.'''
Regards,
Bengt Richter