大小默認為當前位置。當前文件位置不改變。注意,如果一個指定的大小超過了文件的當前大小,其結果是依賴于平臺。
注意:此方法不會在當文件工作在只讀模式打開。
語法
以下是truncate()方法的語法:
fileObject.truncate( [ size ])
參數
size -- 如果可選參數存在,文件被截斷(最多)的大小。
返回值
此方法不返回任何值。
例子
下面的例子顯示 truncate()方法的使用。
#!/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) # Now truncate remaining file. fo.truncate() # Try to read file now line = fo.readline() print "Read Line: %s" % (line) # Close opend file fo.close()
當我們運行上面的程序,它會產生以下結果:
Name of the file: foo.txt Read Line: This is 1st line Read Line:
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com