简体   繁体   中英

Using `touch` in a Windows Python File

I have a Python file running on macOS which calls touch in the following ways:

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

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

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

However, I need to run the script on a Windows machine. How can I modify the script or the system to allow this to be done? Otherwise, I receive the following error:

'touch' is not recognized as an internal or external command, operable program or batch file

Just don't use shell commands. Touch can be done with platform independent pathlib

from pathlib import Path

Path("some/path/file.txt").touch()

Simple open(path, "w") would also work but path on windows would need \\ instead of /

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM