touch是一个Unix实用程序,它将文件的修改和访问时间设置为一天中的当前时间。如果该文件不存在,则使用默认权限创建该文件。
如何将其实现为Python函数?尽量跨平台和完整。
(目前谷歌的“python触摸文件”的结果不是很好,但指向os.utime。)
touch是一个Unix实用程序,它将文件的修改和访问时间设置为一天中的当前时间。如果该文件不存在,则使用默认权限创建该文件。
如何将其实现为Python函数?尽量跨平台和完整。
(目前谷歌的“python触摸文件”的结果不是很好,但指向os.utime。)
当前回答
为什么不试试呢?:
import os
def touch(fname):
try:
os.utime(fname, None)
except OSError:
open(fname, 'a').close()
我相信这消除了任何重要的竞争条件。如果文件不存在,则会抛出异常。
这里唯一可能的竞态条件是,文件是在调用open()之前,但在os.utime()之后创建的。但这并不重要,因为在这种情况下,修改时间将如预期的那样,因为它必须发生在调用touch()期间。
其他回答
为什么不试试呢?:
import os
def touch(fname):
try:
os.utime(fname, None)
except OSError:
open(fname, 'a').close()
我相信这消除了任何重要的竞争条件。如果文件不存在,则会抛出异常。
这里唯一可能的竞态条件是,文件是在调用open()之前,但在os.utime()之后创建的。但这并不重要,因为在这种情况下,修改时间将如预期的那样,因为它必须发生在调用touch()期间。
我有一个用于备份的程序: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种触摸方式。
下面是一些使用ctypes的代码(仅在Linux上测试):
from ctypes import *
libc = CDLL("libc.so.6")
# struct timespec {
# time_t tv_sec; /* seconds */
# long tv_nsec; /* nanoseconds */
# };
# int futimens(int fd, const struct timespec times[2]);
class c_timespec(Structure):
_fields_ = [('tv_sec', c_long), ('tv_nsec', c_long)]
class c_utimbuf(Structure):
_fields_ = [('atime', c_timespec), ('mtime', c_timespec)]
utimens = CFUNCTYPE(c_int, c_char_p, POINTER(c_utimbuf))
futimens = CFUNCTYPE(c_int, c_char_p, POINTER(c_utimbuf))
# from /usr/include/i386-linux-gnu/bits/stat.h
UTIME_NOW = ((1l << 30) - 1l)
UTIME_OMIT = ((1l << 30) - 2l)
now = c_timespec(0,UTIME_NOW)
omit = c_timespec(0,UTIME_OMIT)
# wrappers
def update_atime(fileno):
assert(isinstance(fileno, int))
libc.futimens(fileno, byref(c_utimbuf(now, omit)))
def update_mtime(fileno):
assert(isinstance(fileno, int))
libc.futimens(fileno, byref(c_utimbuf(omit, now)))
# usage example:
#
# f = open("/tmp/test")
# update_mtime(f.fileno())
看起来这是Python 3.4的新功能- pathlib。
from pathlib import Path
Path('path/to/file.txt').touch()
这将在该路径下创建一个file.txt文件。
--
路径。触摸(模式= 0 o777 exist_ok = True) 在这个给定的路径上创建一个文件。如果给出了mode,它将与进程的umask值相结合,以确定文件模式和访问标志。如果文件已经存在,则如果exist_ok为true(并且其修改时间更新为当前时间),则函数成功,否则将引发FileExistsError。
Write_text()从pathlib。路径可以使用。
>>> from pathlib import Path
>>> Path('aa.txt').write_text("")
0