Python Save to file


Question

I would like to save a string to a file with a python program named Failed.py

Here is what I have so far:

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
1
57
10/22/2014 5:40:15 PM

file = open('Failed.py', 'w')
file.write('whatever')
file.close()

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')
137
2/8/2017 2:06:54 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon