Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File modes with '+' character does not give expected output #83116

Closed
anmolkejriwal mannequin opened this issue Nov 28, 2019 · 3 comments
Closed

File modes with '+' character does not give expected output #83116

anmolkejriwal mannequin opened this issue Nov 28, 2019 · 3 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes OS-windows type-bug An unexpected behavior, bug, or error

Comments

@anmolkejriwal
Copy link
Mannequin

anmolkejriwal mannequin commented Nov 28, 2019

BPO 38935
Nosy @pfmoore, @tjguk, @stevendaprano, @zware, @zooba

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2019-11-28.07:29:19.327>
created_at = <Date 2019-11-28.07:17:15.092>
labels = ['3.8', 'invalid', 'type-bug', '3.7', 'OS-windows']
title = "File modes with '+' character does not give expected output"
updated_at = <Date 2019-11-28.07:29:19.314>
user = 'https://bugs.python.org/anmolkejriwal'

bugs.python.org fields:

activity = <Date 2019-11-28.07:29:19.314>
actor = 'steven.daprano'
assignee = 'none'
closed = True
closed_date = <Date 2019-11-28.07:29:19.327>
closer = 'steven.daprano'
components = ['Windows']
creation = <Date 2019-11-28.07:17:15.092>
creator = 'anmolkejriwal'
dependencies = []
files = []
hgrepos = []
issue_num = 38935
keywords = []
message_count = 3.0
messages = ['357609', '357610', '357611']
nosy_count = 6.0
nosy_names = ['paul.moore', 'tim.golden', 'steven.daprano', 'zach.ware', 'steve.dower', 'anmolkejriwal']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue38935'
versions = ['Python 3.6', 'Python 3.7', 'Python 3.8']

@anmolkejriwal
Copy link
Mannequin Author

anmolkejriwal mannequin commented Nov 28, 2019

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

@anmolkejriwal anmolkejriwal mannequin added 3.7 (EOL) end of life 3.8 only security fixes OS-windows type-bug An unexpected behavior, bug, or error labels Nov 28, 2019
@anmolkejriwal
Copy link
Mannequin Author

anmolkejriwal mannequin commented Nov 28, 2019

According to the doc,
https://docs.python.org/3/library/functions.html#open

The expected output for r+ mode in the program mentioned earlier should be:

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

@stevendaprano
Copy link
Member

This is expected behaviour. You read from the position of the file pointer, not the beginning of the file, so after writing to to the file, you have to use the seek() method to return to the beginning if you want to read what you just wrote.

https://docs.python.org/3/library/io.html

https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life 3.8 only security fixes OS-windows type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant