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 10 23629
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
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>
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 ?
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.
-Peter
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>
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 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
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 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.
Which we probably all can. It's a right bugger when you actually have to
listen to what the customer wants, innit? ;-)
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
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>
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
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 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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
On Tue, 23 Aug 2005 08:23:40 -0400, Peter Hansen <pe***@engcorp.com> 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:
'''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 This discussion thread is closed Replies have been disabled for this discussion. Similar topics
33 posts
views
Thread by Frank |
last post: by
|
2 posts
views
Thread by Robert May |
last post: by
|
3 posts
views
Thread by Marek |
last post: by
|
5 posts
views
Thread by Andrew S. Giles |
last post: by
|
4 posts
views
Thread by James |
last post: by
|
1 post
views
Thread by James |
last post: by
|
12 posts
views
Thread by Elmo Mäntynen |
last post: by
|
2 posts
views
Thread by Chuck Anderson |
last post: by
|
8 posts
views
Thread by Nikhil |
last post: by
| | | | | | | | | | |