问题:seek()函数?

请在这里原谅我的困惑,但是我已经阅读了有关python中seek()函数的文档(必须使用它之后),尽管它对我有所帮助,但我对它的实际含义还是有些困惑,但任何解释都很多谢谢,谢谢。

Please excuse my confusion here but I have read the documentation regarding the seek() function in python (after having to use it) and although it helped me I am still a bit confused on the actual meaning of what it does, any explanations are much appreciated, thank you.


回答 0

关于seek()没有太多的担心。

首先,对打开的文件进行操作时非常有用。

重要的是要注意其语法如下:

fp.seek(offset, from_what)

fp您正在使用的文件指针在哪里;offset表示您将移动多少个职位;from_what定义您的参考点:

  • 0:表示您的参考点是文件的开头
  • 1:表示参考点是当前文件位置
  • 2:表示您的参考点是文件的结尾

如果省略,则from_what默认为0。

永远不要忘记,在管理文件时,该文件中总会存在您当前正在处理的位置。刚打开时,该位置是文件的开头,但是当您使用它时,可能会前进。
seek当您需要walk打开该文件时,对您很有用,就像您要进入的路径一样。

Regarding seek() there’s not too much to worry about.

First of all, it is useful when operating over an open file.

It’s important to note that its syntax is as follows:

fp.seek(offset, from_what)

where fp is the file pointer you’re working with; offset means how many positions you will move; from_what defines your point of reference:

  • 0: means your reference point is the beginning of the file
  • 1: means your reference point is the current file position
  • 2: means your reference point is the end of the file

if omitted, from_what defaults to 0.

Never forget that when managing files, there’ll always be a position inside that file where you are currently working on. When just open, that position is the beginning of the file, but as you work with it, you may advance.
seek will be useful to you when you need to walk along that open file, just as a path you are traveling into.


回答 1

当您打开文件时,系统指向文件的开头。您所做的任何读取或写入都将从一开始就发生。一个seek()操作将该指针移动到文件的其他部分,以便您可以在该位置进行读取或写入。

因此,如果您想读取整个文件但跳过前20个字节,请打开文件,seek(20)移至要开始读取的位置,然后继续读取文件。

或者说您想每隔10个字节读取一次,您可以编写一个循环seek(9, 1)(相对于当前位置向前移动9个read(1)字节)(重复读取一个字节)。

When you open a file, the system points to the beginning of the file. Any read or write you do will happen from the beginning. A seek() operation moves that pointer to some other part of the file so you can read or write at that place.

So, if you want to read the whole file but skip the first 20 bytes, open the file, seek(20) to move to where you want to start reading, then continue with reading the file.

Or say you want to read every 10th byte, you could write a loop that does seek(9, 1) (moves 9 bytes forward relative to the current positions), read(1) (reads one byte), repeat.


回答 2

seek期望的功能的偏移以字节为单位。

Ascii文件示例:

因此,如果您的文本文件包含以下内容:

simple.txt

abc

您可以跳过1个字节来跳过第一个字符,如下所示:

fp = open('simple.txt', 'r')
fp.seek(1)
print fp.readline()
>>> bc

二进制文件示例收集宽度:

fp = open('afile.png', 'rb')
fp.seek(16)
print 'width: {0}'.format(struct.unpack('>i', fp.read(4))[0])
print 'height: ', struct.unpack('>i', fp.read(4))[0]

注意:调用后,read您将更改读数头的位置,其作用类似于seek

The seek function expect’s an offset in bytes.

Ascii File Example:

So if you have a text file with the following content:

simple.txt

abc

You can jump 1 byte to skip over the first character as following:

fp = open('simple.txt', 'r')
fp.seek(1)
print fp.readline()
>>> bc

Binary file example gathering width :

fp = open('afile.png', 'rb')
fp.seek(16)
print 'width: {0}'.format(struct.unpack('>i', fp.read(4))[0])
print 'height: ', struct.unpack('>i', fp.read(4))[0]

Note: Once you call read you are changing the position of the read-head, which act’s like seek.


回答 3

对于字符串,请忘记使用WHENCE:使用f.seek(0)放置在文件的开头,使用f.seek(len(f)+1)放置在文件的结尾。使用open(file,“ r +”)读取/写入文件中的任何位置。如果使用“ a +”,则无论将光标放在何处,都只能在文件末尾写入(附加)。

For strings, forget about using WHENCE: use f.seek(0) to position at beginning of file and f.seek(len(f)+1) to position at the end of file. Use open(file, “r+”) to read/write anywhere in a file. If you use “a+” you’ll only be able to write (append) at the end of the file regardless of where you position the cursor.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。