writelines(list)
list : Input data as list . 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
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()
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.