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
Right-click the Python window and select Save As to save your code either as a Python file (. py) or Text file (. txt). If saving to a Python file, only the Python code will be saved.
To write to a text file in Python, you follow these steps: First, open the text file for writing (or append) using the open() function. Second, write to the text file using the write() or writelines() method. Third, close the file using the close() method.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With