Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write to a file immediately?

Tags:

c#

filesystems

I want to keep a logfile, and I am using TextWriter/StreamWriter to write messages to my logfile. I call TextWriter.Flush() after writing the message, but my message still doesn't show up right away. It only shows up when I call Close, and I would rather not reopen and close the file all of the time just to see messages immediately.

like image 256
esac Avatar asked Oct 05 '09 05:10

esac


People also ask

How do you write to a file in Python?

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.

How do you keep old content when writing to a file in Python?

We can keep old content while using write in python by opening the file in append mode. To open a file in append mode, we can use either 'a' or 'a+' as the access mode. The definition of these access modes are as follows: Append Only ('a'): Open the file for writing.

What does flush () do in Python?

The flush() method in Python file handling clears the internal buffer of the file. In Python, files are automatically flushed while closing them. However, a programmer can flush a file before closing it by using the flush() method.


1 Answers

You might want to try constructing a FileStream yourself passing in the FileOptions.WriteThrough to the constructor. Then construct your StreamWriter with that FileStream. The WriteThrough option bypasses the cache.

like image 190
Mike Two Avatar answered Sep 28 '22 02:09

Mike Two