Skip to content

Latest commit

 

History

History
27 lines (20 loc) · 694 Bytes

variable_write_file.md

File metadata and controls

27 lines (20 loc) · 694 Bytes
  • 需要将subprocess处理结果写入文件
f = open( 'file.py', 'w' )
f.write( 'dict = ' + repr(dict) + '\n' )
f.close()
p = subprocess.Popen("cat /etc/redhat-release", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
res = p.communicate()[0]

f = open("/tmp/record.txt",'w')
f.write("%s %s" % (datetime.now(),res.strip()))
f.close()
  • 清空文件内容
open(filename, 'w').close()

参考