Saturday, 5 December 2015

Python: Append data to a file

Open a file in append mode and write data to file.
>>> f=open("/Users/harikrishna_gurram/data.txt", 'r')
>>> f.read()
'first line'
>>> f.close()
>>> 
>>> f=open("/Users/harikrishna_gurram/data.txt", 'a')
>>> f.write("\nSecond line")
12
>>> f.write("\nThird line")
11
>>> f.close()
>>> f=open("/Users/harikrishna_gurram/data.txt")
>>> f.read()
'first line\nSecond line\nThird line'
>>> 
>>> f=open("/Users/harikrishna_gurram/data.txt")
>>> for line in f:
...     print(line, end='')
... 
first line
Second line
Third line


Previous                                                 Next                                                 Home

No comments:

Post a Comment