[New-bugs-announce] [issue38935] File modes with '+' character does not give expected output

Anmol Kejriwal report at bugs.python.org
Thu Nov 28 02:17:15 EST 2019


New submission from Anmol Kejriwal <kejriwalanmol at gmail.com>:

The file modes a+, r+, w+ should read and write both. But a+ and w+ modes are not reading anything from the file. The RUN for the program is successful, but there is no output from the program.
Below is a simple program:

file=open("abcd.txt","w+")
l=["This is python.\nThis is an easy language.\nAnyone can learn this easily"]
file.writelines(l)
file.close()
file=open("abcd.txt","a+")   #Replacing with w+ also doesn't read.
file.write("123")      
t1=file.read()               #This read() does not work.
print(t1)                    #Does not print anything here.
file.close()

In r+ mode, it should write in the file without truncation and read it too. Instead, it is removing from the file, the equal number of characters I am trying to write in the file.
Below is the program:

file=open("abcd.txt","w+")
l=["This is python.\nThis is an easy language.\nAnyone can learn this easily"]
file.writelines(l)
file.close()
file=open("abcd.txt","r+")
file.write("123")
t1=file.read()
print(t1)

Output for this is:

s is python.
This is an easy language.
Anyone can learn this easily

----------
components: Windows
messages: 357609
nosy: anmolkejriwal, paul.moore, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: File modes with '+' character does not give expected output
type: behavior
versions: Python 3.6, Python 3.7, Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38935>
_______________________________________


More information about the New-bugs-announce mailing list