This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Truncate method
Type: behavior Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: File truncate() not defaulting to current position as documented
View: 26158
Assigned To: Nosy List: Cavad Salmanov, serhiy.storchaka, xtreak
Priority: normal Keywords:

Created on 2019-03-13 02:33 by Cavad Salmanov, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg337830 - (view) Author: Cavad Salmanov (Cavad Salmanov) Date: 2019-03-13 02:33
I created a new file for researching truncate() method. and I writed  this text:
alma
armud
heyva
nar
qarpiz
yemis

I wanted to delete all the text apart from first line by truncate method.

I writed these codes on python idle:
__________________________________________
#First, I read text for showing it's content
>>> dosya = open('deneme.txt','r')
>>> dosya.read()
'alma\narmud\nheyva\nnar\nqarpiz\nyemis'
>>> dosya.close()


#Then writing codes for operation which I wanted
>>> dosya = open('deneme.txt','r+')
>>> dosya.readline()
'alma\n'
>>> dosya.tell()
6
>>> dosya.truncate()
38
>>> dosya.seek(0)
0
>>> dosya.read()
'alma\narmud\nheyva\nnar\nqarpiz\nyemis'
>>> dosya.close()
#I thought I must closed the file, then read again:


#Then I saw nothing has changed
>>> dosya = open('deneme.txt','r')
>>> dosya.read()
'alma\narmud\nheyva\nnar\nqarpiz\nyemis'
>>> dosya.close()

#But when I writed codes in this way, it is worked
>>> dosya = open('deneme.txt','r+')
>>> dosya.readline()
'alma\n'
>>> dosya.tell()
6
>>> dosya.seek(6)
6
>>> dosya.truncate()
6
>>> dosya.close()
>>> dosya = open('deneme.txt','r')
>>> dosya.read()
'alma\n'

# I was on 6th byte in both. But it is worked which I used seek() #method. I think both of them were returnden same things
msg337831 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-03-13 02:53
This looks like a similar report to issue26158 where truncate refers to the position of the underlying buffer and could be documented better.
History
Date User Action Args
2022-04-11 14:59:12adminsetgithub: 80459
2019-03-13 03:41:28josh.rsetstatus: open -> closed
superseder: File truncate() not defaulting to current position as documented
resolution: duplicate
stage: resolved
2019-03-13 02:53:11xtreaksetnosy: + serhiy.storchaka, xtreak
messages: + msg337831
2019-03-13 02:33:34Cavad Salmanovcreate