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
1
  • Possible duplicate of stackoverflow.com/questions/12654772/… 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 Commented Dec 7, 2019 at 15:45

1 Answer 1

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 /

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.