Both seems to be working same but there is a catch.
r+ :-
w+ :-
So, Overall saying both are meant to open the file to read and write but difference is whether we want to erase the data in the beginning and then do read/write or just start as it is.
abc.txt
- in beginning
1234567
abcdefg
0987654
1234
Code for r+
with open('abc.txt', 'r+') as f: # abc.txt should exist before opening
print(f.tell()) # Should give ==> 0
f.write('abcd')
print(f.read()) # Pointer is pointing to index 3 => 4th position
f.write('Sunny') # After read pointer is at End of file
Output
0
567
abcdefg
0987654
1234
abc.txt
- After Run:
abcd567
abcdefg
0987654
1234Sunny
Resetting abc.txt as initial
Code for w+
with open('abc.txt', 'w+') as f:
print(f.tell()) # Should give ==> 0
f.write('abcd')
print(f.read()) # Pointer is pointing to index 3 => 4th position
f.write('Sunny') # After read pointer is at End of file
Output
0
abc.txt
- After Run:
abcdSunny