3

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
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
honeymoow
  • 83
  • 6
  • Possible duplicate of https://stackoverflow.com/questions/12654772/create-empty-file-using-python Using open() and other Python handlers is a more efficient way to deal with this problem since it is cross platform, unlike using os.system to call terminal commands – paupaulaz Dec 07 '19 at 15:45

1 Answers1

7

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 /

RafalS
  • 5,834
  • 1
  • 20
  • 25