python - Comment vous appendez à un fichier

Mots clés : pythonfileappend

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

vote vote

100

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

88

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

77

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

70

'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

54

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

Questions similaires