python seek函数whence_file.seek(offset[, whence])
file.seek(offset[, whence])
描述 (Description)
方法seek()設(shè)置文件在偏移處的當(dāng)前位置。 whence參數(shù)是可選的,默認(rèn)為0,表示絕對(duì)文件定位,其他值為1表示相對(duì)于當(dāng)前位置的搜索,2表示相對(duì)于文件結(jié)束的搜索。
沒(méi)有回報(bào)價(jià)值。 請(qǐng)注意,如果打開(kāi)文件以使用“a”或“a +”進(jìn)行追加,則在下次寫入時(shí)將撤消任何seek()操作。
如果僅使用'a'打開(kāi)文件以追加模式寫入,則此方法基本上是無(wú)操作,但對(duì)于在追加模式下打開(kāi)且啟用讀取的文件(模式'a +')仍然有用。
如果使用't'以文本模式打開(kāi)文件,則只有tell()返回的偏移是合法的。 使用其他偏移會(huì)導(dǎo)致未定義的行為。
請(qǐng)注意,并非所有文件對(duì)象都是可搜索的。
語(yǔ)法 (Syntax)
以下是seek()方法的語(yǔ)法 -fileObject.seek(offset[, whence])
參數(shù) (Parameters)offset - 這是文件中讀/寫指針的位置。
whence - 這是可選的,默認(rèn)為0表示絕對(duì)文件定位,其他值為1表示相對(duì)于當(dāng)前位置的搜索,2表示相對(duì)于文件結(jié)束的搜索。
返回值 (Return Value)
此方法不返回任何值。
例子 (Example)
以下示例顯示了seek()方法的用法。Python is a great language
Python is a great language
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "rw+")
print "Name of the file: ", fo.name
# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line
line = fo.readline()
print "Read Line: %s" % (line)
# Again set the pointer to the beginning
fo.seek(0, 0)
line = fo.readline()
print "Read Line: %s" % (line)
# Close opend file
fo.close()
當(dāng)我們運(yùn)行上面的程序時(shí),它產(chǎn)生以下結(jié)果 -Name of the file: foo.txt
Read Line: Python is a great language.
Read Line: Python is a great language.
總結(jié)
以上是生活随笔為你收集整理的python seek函数whence_file.seek(offset[, whence])的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: iOS小技能: 创建渐变色背景(提供渐变
- 下一篇: python vimdiff_vimdi