Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I want to save a string(text format data) to a file with a python module 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 Answer

0 votes
by (108k points)

For achieving your result, you need to use the write() for storing the data in a python module:

file = open('Failed.py', 'w')

file.write('whatever')

file.close()

Here is a more pythonic version, which closes the file without any external commands, even if there was an exception in the wrapped block:

with open('Failed.py', 'w') as file:

    file.write('whatever')

Kick-start your career in Python with the perfect Python Online Course!

Related questions

0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
2 answers
0 votes
1 answer

Browse Categories

...