且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Python 实现触摸?

更新时间:2023-11-21 11:26:28

看起来这是 Python 3.4 的新内容 - pathlib.

Looks like this is new as of Python 3.4 - pathlib.

from pathlib import Path

Path('path/to/file.txt').touch()

这将在路径上创建一个 file.txt.

This will create a file.txt at the path.

--

Path.touch(mode=0o777,exist_ok=True)

Path.touch(mode=0o777, exist_ok=True)

在此给定路径创建一个文件.如果给出了 mode,它会与进程的 umask 值结合来确定文件模式和访问标志.如果文件已经存在,则如果exist_ok 为true(并且其修改时间更新为当前时间),则该函数成功,否则引发FileExistsError.

Create a file at this given path. If mode is given, it is combined with the process’ umask value to determine the file mode and access flags. If the file already exists, the function succeeds if exist_ok is true (and its modification time is updated to the current time), otherwise FileExistsError is raised.