新聞中心
1、說明

我們是自2013年起的成都網(wǎng)站建設(shè)公司,提供網(wǎng)站建設(shè),電商網(wǎng)站設(shè)計(jì)開發(fā),外貿(mào)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)頁設(shè)計(jì),小程序制作、等服務(wù)。為客戶創(chuàng)造有價(jià)值的品牌營銷體驗(yàn),讓互聯(lián)網(wǎng)提升企業(yè)的競爭力!
從文件的首行首字符開始截?cái)啵財(cái)辔募閚個(gè)字符;無n表示從當(dāng)前位置起截?cái)?;截?cái)嘀髇后面的所有字符被刪除。
2、語法
fileObject.TRUNCATE( [ size ])
3、參數(shù)
size,可選,如果存在則文件截?cái)酁?size 字節(jié)。
4、返回值
該方法沒有返回值。
5、實(shí)例
#!/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()當(dāng)我們運(yùn)行上面的程序,它會(huì)產(chǎn)生以下結(jié)果:
Name of the file: foo.txt Read Line: This is 1st line Read Line:
以上就是python truncate方法的介紹,在裁剪文件方面經(jīng)常會(huì)使用到,可以就這種方法先進(jìn)行一個(gè)練習(xí)。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)python教程
(推薦操作系統(tǒng):windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。)
本文標(biāo)題:創(chuàng)新互聯(lián)Python教程:python truncate是什么
URL標(biāo)題:http://m.fisionsoft.com.cn/article/dhcpios.html


咨詢
建站咨詢
