在Unix shell中,我可以这样做以清空文件:
cd /the/file/directory/
:> thefile.ext
我将如何在Python中执行此操作?
就是os.system这里的方式,我不知道该怎么做,因为我必须互相发送2个动作,即thecd和then :>。
在Unix shell中,我可以这样做以清空文件:
cd /the/file/directory/
:> thefile.ext
我将如何在Python中执行此操作?
就是os.system这里的方式,我不知道该怎么做,因为我必须互相发送2个动作,即thecd和then :>。
Answers:
打开文件会创建它,并且(除非设置了append('a'))会用空白将其覆盖,例如:
open(filename, 'w').close()
seek或的文件操作tell,可能会发生奇怪的事情。
open("/the/path/thefile.ext","w")?