Best way to 'touch' a file?

K

Kenneth McDonald

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?

Thanks,
Ken
 
P

Peter Hansen

Kenneth said:
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
 
F

Fredrik Lundh

Peter said:
from path import path
path('myfile').touch()

import os
os.utime('myfile', None)

is a bit shorter, of course.
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>
 
P

Peter Hansen

Fredrik said:
import os
os.utime('myfile', None)

is a bit shorter, of course.

And, depending on your needs, quite ineffective:
Traceback (most recent call last):
True

I guess it depends on whether "touch" implies creation-when-missing, as
with the command line version, or just updating the time.

-Peter
 
F

Fredrik Lundh

Peter said:
And, depending on your needs, quite ineffective:

Traceback (most recent call last):

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>
 
P

Peter Hansen

Fredrik said:
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.

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 OP's needs than he's posted
publicly, you're just guessing too... even if we both agree yours is the
more likely interpretation.

-Peter
 
S

Steve Holden

Peter said:
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 OP'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
 
F

Fredrik Lundh

Peter said:
You've quoted selectively. He also said "Unix-style 'touch'", from
which one could quite legitimately infer

nope. read his post again.

</F>
 
P

Peter Hansen

Fredrik said:
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
 
S

Steve Holden

Peter said:
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 OP'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
OP can choose whichever best meets his real requirements, whether they
were accurately stated or not.

regards
Steve
 
B

Bengt Richter

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.
OTOH, 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.
Otherwise 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top