简体   繁体   English

求function Python

[英]Seek function Python

When I use the seek function, I am unable to set the whence parameter to 1. After some searching I found that I need to include a "byte--like object" so that I can set the whence parameter to 1. What does this entail, and do I always need a bytes-like object so that I can set the whence to 1 and have the offset starting from the current index position in a file?当我使用seek function时,无法将whence参数设置为1。经过一番搜索,我发现我需要包含一个“类似字节的对象”,以便我可以将whence参数设置为1。这是什么需要,我是否总是需要一个类似字节的 object 以便我可以将 wherece 设置为 1 并从文件中的当前索引 position 开始偏移?

When you open file in text mode, the file-like object it returns is io.TextIOWrapper (derived from io._TextIOBase ).当您以文本模式open文件时,它返回的类似 object 的文件是io.TextIOWrapper (派生自io._TextIOBase )。 Their seek() with regard to whence works as follows:他们的seek()关于whence的工作如下:

The default value for whence is SEEK_SET. wherece 的默认值是 SEEK_SET。

SEEK_SET or 0 : seek from the start of the stream (the default); SEEK_SET0 :从 stream 的开头开始搜索(默认); offset must either be a number returned by TextIOBase.tell() , or zero. offset 必须是TextIOBase.tell()返回的数字或零。 Any other offset value produces undefined behaviour.任何其他偏移值都会产生未定义的行为。

SEEK_CUR or 1 : “seek” to the current position; SEEK_CUR1 :“搜索”到当前的 position; offset must be zero, which is a no-operation (all other values are unsupported). offset 必须为零,这是一个无操作(不支持所有其他值)。

SEEK_END or 2 : seek to the end of the stream; SEEK_END2 :寻找到 stream 的末尾; offset must be zero (all other values are unsupported).偏移量必须为零(不支持所有其他值)。

Ie you can use whence values of 1 and 2 only with offset of 0 .即,您只能在offset0的情况下使用whence12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM