473,687 Members | 3,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 24009
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('myfil e', 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('myfil e', None)

is a bit shorter, of course.


And, depending on your needs, quite ineffective:
import os
os.utime('missi ng.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.f ile').touch()
path('missing.f ile').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('missi ng.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.f ile').touch()
path('missing.f ile').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
dependencie s.

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
9858
by: Frank | last post by:
What is the best IDE for developing PHP applications? What do you use and why? Thanks.
2
1942
by: Robert May | last post by:
I have an application that processes text strings using information in a file. I want to deploy this app using no-touch deployment. The config files and everything else are deployed just fine, but the CGT file (compiled Grammar Table) is not, and the program crashes when it attempts to read this file. Is there any way to have this file loaded as needed? With no touch deployment, how do you copy files that aren't assemblies down to...
3
2172
by: Marek | last post by:
Hi there, my WinForm application has to be installed in "no-touch-deployment" mode, so users can run it from the web. I need to read application's .config file but it does not work in no-touch-deployment. Could anyone give me directions to documentation piece (or article) that explains how to implement app .config file reading in no-touch-deployment ? (just to clarif the issue, my app is not ASP.NET) I'd appreciate any help, thank you....
5
5049
by: Andrew S. Giles | last post by:
I thought I would post here, as I am sure someone, somewhere has run into this problem, and might have a good solution for me. I am writing an applicaiton in C# that will accept data and then put it into an Excel spreadsheet. Easy, right? Well it is, until you have to get the data from another application that is written in Borland C++ PowerBuilder 5. The situation is that the Borland Code isnt going to get re-written (too expensive,...
4
3523
by: James | last post by:
I have a VB windows forms application that accesses a Microsoft Access database that has been secured using user-level security. The application is being deployed using No-Touch deployment. The objective in utilizing this new deployment method is to reduce the maintenance overhead as well as making it easier for my users to setup and run the application initially. I have VS 2002, Windows XP, Access XP(2000 format). He is my problem....
1
650
by: James | last post by:
I have a VB windows forms application that accesses a Microsoft Access database that has been secured using user-level security. The application is being deployed using No-Touch deployment. The objective in utilizing this new deployment method is to reduce the maintenance overhead as well as making it easier for my users to setup and run the application initially. I have VS 2002, Windows XP, Access XP(2000 format). He is my problem....
12
5438
by: Elmo Mäntynen | last post by:
Is there something better than using fnctl? It seems a bit intimidating with a quick look.
2
2213
by: Chuck Anderson | last post by:
I've got a script that "syncs" files from my working hard drive (Windows XP) to a backup drive based on filesize and filemtime. It's never had problems at the DST switch before, but since upgrading to Php5, it seems like Php is confused (is certainly confusing me) as to what time zone to use. I am in Denver and I have set the default timezone to "America/Denver" in php.ini (... would much prefer using numbers like -0700 or -0600). The...
8
16204
by: Nikhil | last post by:
what are the simple ways? I could think of os.open(), os.exec(touch file) are there any simpler methods?
0
8590
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8524
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9066
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8779
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8783
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4321
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2960
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1947
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.