writelines(list)
list : Input data as list .
Using 'w' mode
Details of different mode of file opening is shown here.
fob=open('data5.txt','w')
my_list=['Alex','Ronald','John']
fob.writelines(my_list)
fob.close()
# read data after writing
fob=open('data5.txt','r')
print(fob.read()) # AlexRonaldJohn
Using 'a' mode
We used the same file as written above. As the mode of file open is 'a' , the data will be added at the end of the file.
fob=open('data5.txt','a')
my_list=['One','Two','Three']
fob.writelines(my_list)
fob.close()
fob=open('data5.txt','r')
print(fob.read()) # AlexRonaldJohnOneTwoThree
By using tell() method we can get the file object position. Here is the code to understand file position after writelines()
fob=open('data5.txt','w')
my_list=['Alex','Ronald','John']
fob.writelines(my_list)
print(fob.tell()) # 14
fob.close()
# read data after writing
fob=open('data5.txt','r')
print(fob.tell()) # 0
print(fob.read()) # AlexRonaldJohn
fob=open('data5.txt','a')
my_list=['One','Two','Three']
fob.writelines(my_list)
print(fob.tell()) # 25
fob.close()
fob=open('data5.txt','r')
print(fob.tell()) # 0
print(fob.read()) # AlexRonaldJohnOneTwoThree
writable() »
« File Append
File Write »
← Subscribe to our YouTube Channel here