Python 2.7 : Write to file instantly
You can use flush()
or you can set the file object to be unbuffered.
Details on using that parameter for open()
here.
So you would change your open call to -
outputFile = open("./outputFile.txt", "a", 0)
How to create a text file with python 2.7
Call this in the same directory as your script in command prompt.
python your_script.py > output.txt
If you want to do this within the script itself:
with open("output.txt", "w") as f:
f.write("Random Number")
Writing to file during infinite while
To sum up all the answers in one:
try:
with open('log.txt', 'a') as fichier:
while True:
for hostname in hostnames:
ping = os.system(" Ping " + str(hostname))
if ping == 1:
print("DOWN")
fichier.flush()
fichier.write(str(date) + " " + str(hostname) + '\n' + '\n')
else:
print("UP")
except KeyboardInterrupt:
print("Done!")
Correct way to write line to file?
This should be as simple as:
with open('somefile.txt', 'a') as the_file:
the_file.write('Hello\n')
From The Documentation:
Do not use
os.linesep
as a line terminator when writing files opened in text mode (the default); use a single'\n'
instead, on all platforms.
Some useful reading:
- The
with
statement open()
'a'
is for append, or use'w'
to write with truncation
os
(particularlyos.linesep
)
how to print directly to a text file in both python 2.x and 3.x?
You can use the print_function
future import to get the print()
behaviour from python3 in python2:
from __future__ import print_function
with open('filename', 'w') as f:
print('some text', file=f)
If you do not want that function to append a linebreak at the end, add the end=''
keyword argument to the print()
call.
However, consider using f.write('some text')
as this is much clearer and does not require a __future__
import.
Python 2.7: Print to File
If you want to use the print
function in Python 2, you have to import from __future__
:
from __future__ import print_function
But you can have the same effect without using the function, too:
print >>f1, 'This is a test'
How to make python write on a file live
You should be able to write using the flush() function.
outputFile.flush()
More informations on the flush() function on this link: https://docs.python.org/2/library/functions.html#print
That's nearly the same request than this one: Python 2.7 : Write to file instantly
Related Topics
How to Find Which Columns Contain Any Nan Value in Pandas Dataframe
Efficient Way to Remove Keys with Empty Strings from a Dict
I Have Python on My Ubuntu System, But Gcc Can't Find Python.H
Browse Files and Subfolders in Python
How to Change the String Representation of a Python Class
Login to Website Using Urllib2 - Python 2.7
Find the Indexes of All Regex Matches
How to Convert a Scikit-Learn Dataset to a Pandas Dataset
How to Make Sessions Timeout in Flask
Convert a Python Dict to a String and Back
Format Strings VS Concatenation
Monkey Patching a Class in Another Module in Python
Removing Control Characters from a String in Python
How to Check If One Dictionary Is a Subset of Another Larger Dictionary
Pandas: Resample Timeseries with Groupby
How to Force/Ensure Class Attributes Are a Specific Type