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
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.