touch是一个Unix实用程序,它将文件的修改和访问时间设置为当前时间.如果该文件不存在,则使用默认权限创建该文件.
你如何将它作为Python函数实现?尝试跨平台和完整.
(目前Google搜索"python touch file"的结果并不是那么好,但是指向os.utime.)
我想在Unix环境中使用Python脚本创建空文件.可以看到提到实现相同的不同方式.一个人比另一个人有什么好处/陷阱.
os.system('touch abc')
open('abc','a').close()
open('abc','a')
subprocess.call(['touch','abc'])
我有一个在 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
我想在Python中创建一个空文件.但我很难创建一个..Below是我用来创建的Python代码,但它给了我错误:
gdsfile  = "/home/hha/temp.gds"
if not (os.path.isfile(gdsfile)):
    os.system(touch gdsfile)
我得到的错误:文件"/home/hha/bin/test.py",第29行os.system(touch gdsfile)^ SyntaxError:语法无效
但是从命令行,我可以使用:touch创建新文件
非常感谢你的帮助霍华德