python - Comment vous appendez à un fichier

Mots clés : pythonfileappend

meilleur 5 Réponses python - Comment vous appendez à un fichier

vote vote


with open("test.txt", "a") as myfile:     myfile.write("appended text") 
vote vote


>>> with open('test1','wb') as f:         f.write('test') >>> with open('test1','ab') as f:         f.write('koko') >>> with open('test1','rb') as f: 'testkoko' 
>>> f = open('test','a+') # Not using 'with' just to simplify the example REPL session >>> f.write('hi') >>> >>> 'hi' >>> >>> f.write('bye') # Will still append despite the seek(0)! >>> >>> 'hibye' 
fseek(stream, 0, SEEK_END); 
>>> open("test","wb").write("test") >>> open("test","a+b").write("koko") >>> open("test","rb").read() 'testkoko' 
vote vote


f = open('filename.txt', 'a') f.write("stuff") f.close() 
vote vote


'w'   write text 'r'   read text 'a'   append text 
f = open('filename.txt', 'a')  f.write('whatever you want to write here (in append mode) here.') 
'r+'  read + write text 'w+'  read + write text 'a+'  append + read text 
'rb'  read binary 'wb'  write binary 'ab'  append binary 'rb+' read + write binary 'wb+' read + write binary 'ab+' append + read binary 
vote vote


with open("foo", "a") as f:     f.write("cool beans...") 

Questions similaires