0

r+ in python

with open("test2.txt", "r+") as myfile: m = myfile.write("ssss") print(myfile.read()) output = Why does this output appear ?As it should be: ssss

21st Apr 2020, 4:07 PM
Ahmad Tarek
Ahmad Tarek - avatar
6 Respostas
+ 3
put: myfile.seek(0) Above print(myfile.read())
21st Apr 2020, 4:30 PM
Slick
Slick - avatar
+ 1
Slick Thank you❤️ It worked
21st Apr 2020, 4:38 PM
Ahmad Tarek
Ahmad Tarek - avatar
+ 1
The tests ive run it seems like it does overwrite the file. If you dont want to overwrite, you can always create a blank file, then in python: myfile = open('filepath', 'a+') This will append any data written to the file after
21st Apr 2020, 5:02 PM
Slick
Slick - avatar
+ 1
Not any way that i know of. I usually use it like file.seek(0) to go the the begging and file.seek(2) to go to the end. you can do something sort of like mad libs though. If you already know the majority of whats going into a file, you can format a variable into whatever you want to write in your file. dinner = input('What would you like for dinner? ') msg = f'I sure would love {dinner} tonight. It's my favorite!' file = open('filepath', 'a') file.write(msg) Thats the best ive got
21st Apr 2020, 5:42 PM
Slick
Slick - avatar
0
Slick Does the r+ overwrite the file
21st Apr 2020, 4:57 PM
Ahmad Tarek
Ahmad Tarek - avatar
0
Slick Can I seek this to write in the middle of the file?
21st Apr 2020, 5:25 PM
Ahmad Tarek
Ahmad Tarek - avatar