touch
是一种Unix实用程序,它将文件的修改和访问时间设置为当天的当前时间。如果该文件不存在,则会使用默认权限创建。
您如何将其实现为Python函数?请尝试跨平台并完整。
(当前搜索“python touch file”的 Google 结果并不理想,但指向 os.utime。)
复杂(可能存在缺陷):
def utime(fname, atime=None, mtime=None)
if type(atime) is tuple:
atime, mtime = atime
if atime is None or mtime is None:
statinfo = os.stat(fname)
if atime is None:
atime = statinfo.st_atime
if mtime is None:
mtime = statinfo.st_mtime
os.utime(fname, (atime, mtime))
def touch(fname, atime=None, mtime=None):
if type(atime) is tuple:
atime, mtime = atime
open(fname, 'a').close()
utime(fname, atime, mtime)
此功能试图也允许设置访问或修改时间,就像GNU touch一样。
创建一个包含所需变量的字符串,并将其传递给os.system可能看起来很合乎逻辑:
touch = 'touch ' + dir + '/' + fileName
os.system(touch)
这种方法在很多方面都是不足的(例如,它不能处理空格),所以不要使用它。
更健壮的方法是使用 subprocess:
subprocess.call(['touch', os.path.join(dirname, fileName)])
虽然这比使用子 shell(os.system)要好得多,但仍只适用于快速脚本;对于跨平台程序,请使用被接受的答案。
subprocess.call(['touch', os.path.join(dirname, fileName)])
比使用子Shell(使用os.system
)要好得多。但是,仍然只在快速且粗略的脚本中使用它,在跨平台程序中请使用被接受的答案。 - aykepathlib.Path
中的write_text()
来完成。>>> from pathlib import Path
>>> Path('aa.txt').write_text("")
0
touch
不会覆盖现有内容。 - Connor Clark我有一个用于备份的程序:https://stromberg.dnsalias.org/~strombrg/backshift/
我使用vmprof对其进行了分析,并确定touch是其中耗时最长的部分。
因此,我研究了快速触摸文件的方法。
我发现在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存储库: https://stromberg.dnsalias.org/svn/touch/trunk 它包括我尝试的4种触摸方式。
还有一个用于触摸的Python模块
>>> from touch import touch
>>> touch(file_name)
pip install touch
进行安装。#!/usr/bin/env python
import sys
inputfile = sys.argv[1]
with open(inputfile, 'r+') as file:
pass
Python新建文件.py foobar.txt
或者
使用子进程:
import subprocess
subprocess.call(["touch", "barfoo.txt"])
touch
功能,而不是如何从头开始重新实现它。对于那些人来说,最好向下滚动到pathlib
解决方案。尽管现在内置了此功能,但是这个答案在“python touch file”的谷歌排名上比相关文档更高。 - Miles