在 Windows Python 文件中使用 `touch`

hon*_*oow 1 python unix windows

我有一个在 macOS 上运行的 Python 文件,它通过以下方式调用 touch:

os.system("touch -c %s" % apicache_file)

os.system("touch -c %s" % downloadFilename)

os.system("touch -c %s" % meta_cache_file)
Run Code Online (Sandbox Code Playgroud)

但是,我需要在 Windows 机器上运行脚本。如何修改脚本或系统以允许执行此操作?否则,我会收到以下错误:

'touch' is not recognized as an internal or external command, operable program or batch file
Run Code Online (Sandbox Code Playgroud)

Raf*_*alS 5

只是不要使用shell命令。触摸可以在独立于平台的情况下完成pathlib

from pathlib import Path

Path("some/path/file.txt").touch()
Run Code Online (Sandbox Code Playgroud)

简单open(path, "w")也可以,但 Windows 上的路径需要\而不是/