Write unbuffered on python 3

2024/9/8 10:31:14

I'm trying to create a file on python without buffer, so its written at the same time I use write(). But for some reason I got an error.
This is the line I'm using:

my_file = open("test.txt", "a", buffering=0) my_file.write("Testing unbuffered writing\n")

And this is the error I got:
my_file = open("test.txt", "a", buffering=0) ValueError: can't have unbuffered text I/O

There is anyway to do an unbuffered write on a file? I'm using python 3 on pyCharm.
Thanks

Answer

The error isn't from Pycharm.

From Python documentation:

buffering is an optional integer used to set the buffering policy.Pass 0 to switch buffering off (only allowed in binary mode)

Your code only works in Python 2 but won't work in Python 3. Because Strings are immutable sequences of Unicode code points in Python 3. You need to have bytes here. To do it in Python 3 you can convert your unicode str to bytes in unbuffered mode.

For example:

my_file.write("Testing unbuffered writing\n".encode("utf-8"))
https://en.xdnf.cn/q/73296.html

Related Q&A

which should I use (for python-based sites)? sass, compass, switchcss...alternatives? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic…

Invalid tag name error when creating element with lxml in python

I am using lxml to make an xml file and my sample program is :from lxml import etree import datetime dt=datetime.datetime(2013,11,30,4,5,6) dt=dt.strftime(%Y-%m-%d) page=etree.Element(html) doc=etree.E…

Method replacement at runtime not updating Private attributes

I understood how to replace methods at run time in Python by going through these links.[ Link1 , Link2 , & Link3].When I replaced a "update_private_variable" method of class A, its gettin…

sys.path and sys.executable is incorrect in jupyter, but no applied fix is working

Ive configured jupyter to be used from a remote computer and set a password to it while initial anaconda setup. Then after fixing this issue, I am trapped in another one. sys.path and sys.executable is…

How to multicolour text with ScrolledText widget?

from tkinter import * from tkinter.scrolledtext import ScrolledTextwindow= Tk() window.geometry(970x45) box = ScrolledText(window, width=70, height=7).pack() box.insert(END, "Ehila") #this in…

Binding Return to button is not working as expected

I bound the event <Return> to a Button, thinking that this would cause the command to be run after hitting Enter:Button(self.f, text="Print", command=self.Printer).pack(side=RIGHT, padx…

ZMQ Pub-Sub Program Failure When Losing Network Connectivity

I have a simple pub-sub setup on a mid-sized network, using ZMQ 2.1. Although some subscribers are using C# bindings, others are using Python bindings, and the issue Im having is the same for either.I…

replacing html tags with BeautifulSoup

Im currently reformatting some HTML pages with BeautifulSoup, and I ran into bit of a problem.My problem is that the original HTML has things like this:<li><p>stff</p></li>and &…

LightGBM: train() vs update() vs refit()

Im implementing LightGBM (Python) into a continuous learning pipeline. My goal is to train an initial model and update the model (e.g. every day) with newly available data. Most examples load an alread…

GTK: create a colored regular button

How do I do it? A lot of sites say I can just call .modify_bg() on the button, but that doesnt do anything. Im able to add an EventBox to the button, and add a label to that, and then change its color…