Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Windows Python文件中使用“touch”_Python_Windows_Unix - Fatal编程技术网

在Windows Python文件中使用“touch”

在Windows Python文件中使用“touch”,python,windows,unix,Python,Windows,Unix,我有一个运行在macOS上的Python文件,它通过以下方式调用touch: os.system("touch -c %s" % apicache_file) os.system("touch -c %s" % downloadFilename) os.system("touch -c %s" % meta_cache_file) 但是,我需要在Windows计算机上运行脚本。如何修改脚本或系统以允许执行此操作?否则,我将收到以下错误: 'touch' is not recognized

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

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

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

os.system("touch -c %s" % meta_cache_file)
但是,我需要在Windows计算机上运行脚本。如何修改脚本或系统以允许执行此操作?否则,我将收到以下错误:

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

只是不要使用shell命令。可使用平台无关的
pathlib

from pathlib import Path

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

简单的
open(path,“w”)
也可以工作,但windows上的path需要
\
而不是
/

,只是不要使用shell命令。可使用平台无关的
pathlib

from pathlib import Path

Path("some/path/file.txt").touch()
简单的
open(path,“w”)
也可以,但windows上的path需要
\
而不是
/

使用open()和其他Python处理程序的可能重复是处理此问题的更有效方法,因为它是跨平台的,与使用os.system调用终端命令不同,使用open()和其他Python处理程序的可能重复使用是处理此问题的更有效的方法,因为它是跨平台的,与使用os.system调用终端命令不同