“触摸”文件的最佳方式? [英] Best way to 'touch' a file?

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

问题描述

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


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

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

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

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

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


谢谢,


解决方案

Kenneth McDonald写道:

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

我正在寻找一种简单的方法来执行UNIX风格的触摸,以更新文件的修改时间而不实际修改它。我可以做某事(我想),比如打开文件进行追加,然后立即关闭它,但这似乎不是一个好主意 - 如果文件是
怎么办?已经开放阅读或写作?有人知道一个不错的,优雅的解决方案吗?



来自路径导入路径的


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


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

,更不用说实际上了。)


-Peter


Peter Hansen写道:

来自路径导入路径
路径(''myfile'')。touch()




import os

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


当然有点短。

help(os.utime)



内置函数的帮助utime:


utime (...)

utime(路径,(atime,utime))

utime(路径,无)


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

使用

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

时间。


< / F>

blockquote>

Fredrik Lundh写道:

Peter Hansen写道:

从路径导入路径
路径(''myfile'')。touch()



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

当然有点短。




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

import os
os.utime(''missing.file'',None)
Traceback(最近一次)最后呼叫):

文件"< stdin>",第1行,在?

OSError:[Errno 2]没有这样的文件或目录:''缺失.file''

来自路径导入路径
路径(''missing.file'')。touch()
path(''missing.file'' ).exists()



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

解决方案

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


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆