本文介绍了“触摸”文件的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发誓python有这样一个命令,但现在我找不到了......


我正在寻找一种简单的方法来执行一个UNIX风格的触摸,

更新文件的修改时间而不实际修改它。

我可以做一些事情(我想)就像打开文件一样附加

然后立即关闭它,但这似乎不是一个好的

想法 - 如果文件已经打开阅读或写作怎么办?任何人都知道一个漂亮,优雅的解决方案吗?


谢谢,


解决方案



来自路径导入路径的


路径(''myfile'')。触摸()


(这依赖于Jason Orendorff的path.py模块,它可以更加优雅地实现这一点和

,更不用说实际上了。)


-Peter





import os

os.utime(''myfile '',无)


当然有点短。



内置函数的帮助utime:


utime (...)

utime(路径,(atime,utime))

utime(路径,无)


Set文件到给定值的访问和修改时间。如果

使用

秒表,请将访问次数和修改时间设置为当前

时间。


< / F>

blockquote>



导入os
os.utime(''myfile'',没有)

当然有点短。




而且,根据你的需要,效率很低:



True


我想这取决于是否触摸暗示创建时失踪,使用命令行版本,或者只是更新时间。


-Peter


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

解决方案



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




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>




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

is a bit shorter, of course.



And, depending on your needs, quite ineffective:


True

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

-Peter


这篇关于“触摸”文件的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 08:16