469,271 Members | 1,009 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Best way to 'touch' a file?

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
Aug 21 '05 #1
10 23255
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
Aug 21 '05 #2
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>

Aug 22 '05 #3
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
Aug 22 '05 #4
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>

Aug 22 '05 #5
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
Aug 23 '05 #6
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/

Aug 23 '05 #7
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>

Aug 23 '05 #8
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
Aug 23 '05 #9
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/

Aug 23 '05 #10
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
Aug 23 '05 #11

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
5 posts views Thread by Andrew S. Giles | last post: by
12 posts views Thread by Elmo Mäntynen | last post: by
2 posts views Thread by Chuck Anderson | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.