touch是一个Unix实用程序,它将文件的修改和访问时间设置为一天中的当前时间。如果该文件不存在,则使用默认权限创建该文件。

如何将其实现为Python函数?尽量跨平台和完整。

(目前谷歌的“python触摸文件”的结果不是很好,但指向os.utime。)


当前回答

以下是充分的:

import os
def func(filename):
    if os.path.exists(filename):
        os.utime(filename)
    else:
        with open(filename,'a') as f:
            pass

如果你想设置一个特定的触摸时间,使用操作系统。使用时间如下:

os.utime(filename,(atime,mtime))

这里,atime和mtime都应该是int/float,并且应该等于epoch time(以秒为单位)到你想设置的时间。

其他回答

创建一个包含所需变量的字符串,并将其传递给os.system似乎是合乎逻辑的:

touch = 'touch ' + dir + '/' + fileName
os.system(touch)

这在很多方面是不够的(例如,它不能处理空白),所以不要这样做。

一个更健壮的方法是使用subprocess:

子流程。([‘碰’,os.path打电话。加入(目录名,文件名)])

虽然这比使用subshell (os.system)要好得多,但它仍然只适用于快速和肮脏的脚本;使用跨平台程序的公认答案。

我有一个用于备份的程序:https://stromberg.dnsalias.org/~strombrg/backshift/

我使用vmprof对它进行了分析,发现到目前为止,触摸是最耗时的部分。

所以我研究了快速接触文件的方法。

我发现在CPython 3.11上,这是最快的:

def touch3(filename, flags=os.O_CREAT | os.O_RDWR):                                                                                  
    """Touch a file using os.open+os.close - fastest on CPython 3.11."""                                                             
    os.close(os.open(filename, flags, 0o644))    

                                                                                

在Pypy3 7.3.9上,这是最快的:

def touch1(filename):                                                                                                                
    """Touch a file using pathlib - fastest on pypy3, and fastest overall."""                                                        
    Path(filename).touch()                                                                                                           

在这两者中,pypy3的最佳性能仅略快于cpython的最佳性能。

我可能有一天会创建一个关于这个的网页,但现在我所拥有的只是一个Subversion repo: https://stromberg.dnsalias.org/svn/touch/trunk 它包括我尝试过的4种触摸方式。

def touch(fname):
    if os.path.exists(fname):
        os.utime(fname, None)
    else:
        open(fname, 'a').close()

这个方法试图比其他解决方案更无种族限制。(with关键字是Python 2.5中新增的。)

import os
def touch(fname, times=None):
    with open(fname, 'a'):
        os.utime(fname, times)

大致相当于这个。

import os
def touch(fname, times=None):
    fhandle = open(fname, 'a')
    try:
        os.utime(fname, times)
    finally:
        fhandle.close()

现在,要真正使其无竞争,您需要使用futimes并更改打开文件句柄的时间戳,而不是打开文件然后更改文件名上的时间戳(可能已重命名)。不幸的是,Python似乎没有提供一种不通过ctypes或类似的方法来调用futimes…


EDIT

正如Nate Parsons所指出的,Python 3.3将为os.supports_fd等函数添加指定文件描述符(当os.supports_fd时)。Utime,它将使用futimes系统调用,而不是在底层使用utimes系统调用。换句话说:

import os
def touch(fname, mode=0o666, dir_fd=None, **kwargs):
    flags = os.O_CREAT | os.O_APPEND
    with os.fdopen(os.open(fname, flags=flags, mode=mode, dir_fd=dir_fd)) as f:
        os.utime(f.fileno() if os.utime in os.supports_fd else fname,
            dir_fd=None if os.supports_fd else dir_fd, **kwargs)

简单的:

def touch(fname):
    open(fname, 'a').close()
    os.utime(fname, None)

打开确保那里有一个文件 utime确保时间戳被更新

从理论上讲,可能有人会在打开文件后删除该文件,导致utime引发异常。但可以说这没什么,因为坏事确实发生了。