+ 1

In which mode does the 'with' function open a file?

can v use 'with' keyword to wright, read or append?

11th Sep 2016, 4:36 AM
Anushree SS
Anushree SS - avatar
3 Réponses
+ 1
if we don't mention anything what mode does it open in?
11th Sep 2016, 5:09 AM
Anushree SS
Anushree SS - avatar
+ 1
'r' (read) is default mode. For other modes we may do something like this: with open(filename, 'w') as wfile: wfile.write(newline)
11th Sep 2016, 5:19 AM
Textobot
Textobot - avatar
0
if you dont mention anything, it will be read mode. "r". if you want read, write, or append in binary, you add a letter b. "rb", "wb", "ab". it is very useful, when you want to write a structure(list, or dictionary) in a file, and later you want your program to use this data. in that case you can use json, and it works only if you open, read or write the file in binary. Excuse my poor english, i hope you understand.
11th Nov 2016, 3:59 PM
Benedek Máté Tóth
Benedek Máté Tóth - avatar