本文介绍了Python 保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用名为 Failed.py
这是我目前所拥有的:
myFile = open('today','r')
ips = {}
for line in myFile:
parts = line.split(' ')
if parts[1] == 'Failure':
if parts[0] in ips:
ips[pars[0]] += 1
else:
ips[parts[0]] = 0
for ip in [k for k, v in ips.iteritems() if v >=5]:
#write to file called Failed.py
推荐答案
file = open('Failed.py', 'w')
file.write('whatever')
file.close()
这是一个更pythonic的版本,它会自动关闭文件,即使包裹的块中有异常:
Here is a more pythonic version, which automatically closes the file, even if there was an exception in the wrapped block:
with open('Failed.py', 'w') as file:
file.write('whatever')
这篇关于Python 保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!