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.
Created on 2018年11月23日 19:35 by liugang93, last changed 2022年04月11日 14:59 by admin. This issue is now closed.
| Messages (3) | |||
|---|---|---|---|
| msg330357 - (view) | Author: liugang (liugang93) | Date: 2018年11月23日 19:35 | |
-- run in Windows 10
1 -
f = open('test', "w+")
f.write('xxx\nyyy\nzzz')
f.seek(0)
f.readline()
print(f.tell()) # output 5 (xxx\r\n)
x = f.truncate()
print(x) # output 13 (xxx\r\nyyy\r\nzzz), why it is 13, not 5?
2 -
f = open('test', "w+")
f.write('xxx\nyyy\nzzz')
f.seek(0)
f.readline()
print(f.tell()) # output 5 (xxx\r\n)
f.seek(f.tell())
x = f.truncate()
print(x) # output 5
|
|||
| msg330358 - (view) | Author: liugang (liugang93) | Date: 2018年11月23日 19:44 | |
# Run in Windows 10
# Code snippet 1
f = open('test', "w+")
f.write('xxx\nyyy\nzzz')
f.seek(0)
f.readline()
print(f.tell()) # 5
x = f.truncate()
print(x) # 13 - why it is 13, not 5?
# Code snippet 2
f = open('test', "w+")
f.write('xxx\nyyy\nzzz')
f.seek(0)
f.readline()
print(f.tell()) # 5
f.seek(f.tell())
x = f.truncate()
print(x) # 5
|
|||
| msg330368 - (view) | Author: Martin Panter (martin.panter) * (Python committer) | Date: 2018年11月24日 01:11 | |
This is the same as Issue 26158. Truncating text files is not clearly documented for a start, and truncating after reading doesn’t seem to be considered much in the implementations. Your question is answered at <https://bugs.python.org/issue26158#msg258619>. Your code calls the C implementation of "io.TextIOWrapper.truncate". This implementation does not consider that there are 8 unread bytes in a buffer, so it truncates the file at the end of the buffer (5 read + 8 unread = 13 bytes). |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:59:08 | admin | set | github: 79485 |
| 2018年11月24日 01:11:07 | martin.panter | set | status: open -> closed superseder: File truncate() not defaulting to current position as documented nosy: + martin.panter messages: + msg330368 resolution: duplicate stage: resolved |
| 2018年11月23日 19:44:37 | liugang93 | set | messages: + msg330358 |
| 2018年11月23日 19:35:08 | liugang93 | create | |