truncate(limit)
limit : (optional) upper limit of resizing the file. fob=open('data.txt','a')
fob.truncate()
fob.close()
# read the file
fob=open('data.txt','r')
print(fob.read())
Output is here ( used the data.txt file given at file read() )
This is first line
This is second line
This is third line
This is fourth line
In the above code we have not used limit so in mode = 'a' file pointer remains at the end of the file. So no data is truncated.
fob=open('data.txt','a')
fob.truncate(13)
fob.close()
# read the file
fob=open('data.txt','r')
print(fob.read()) # This is first
fob=open('data.txt','w')
fob.truncate(10)
fob.close()
# read the file
fob=open('data.txt','r')
print(fob.read())
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.