• <strike id="6sogq"><s id="6sogq"></s></strike>
  • <strike id="6sogq"></strike>

    千鋒教育-做有情懷、有良心、有品質的職業教育機構

    400-811-9990
    手機站
    千鋒教育

    千鋒學習站 | 隨時隨地免費學

    千鋒教育

    掃一掃進入千鋒手機站

    領取全套視頻
    千鋒教育

    關注千鋒學習站小程序
    隨時隨地免費學習課程

    上海
    • 北京
    • 鄭州
    • 武漢
    • 成都
    • 西安
    • 沈陽
    • 廣州
    • 南京
    • 深圳
    • 大連
    • 青島
    • 杭州
    • 重慶
    當前位置:成都千鋒IT培訓  >  技術干貨  >  pythontruncate是什么

    pythontruncate是什么

    來源:千鋒教育
    發布人:xqq
    時間: 2023-11-08 18:44:23

    pythontruncate是什么

    1、說明

    從文件的首行首字符開始截斷,截斷文件為n個字符;無n表示從當前位置起截斷;截斷之后n后面的所有字符被刪除。

    2、語法

    fileObject.truncate([size])

    3、參數

    size,可選,如果存在則文件截斷為size字節。

    4、返回值

    該方法沒有返回值。

    5、實例

    #!/usr/bin/python

    #Openafile

    fo=open("foo.txt","rw+")

    print"Nameofthefile:",fo.name

    #Assumingfilehasfollowing5lines

    #Thisis1stline

    #Thisis2ndline

    #Thisis3rdline

    #Thisis4thline

    #Thisis5thline

    line=fo.readline()

    print"ReadLine:%s"%(line)

    #Nowtruncateremainingfile.

    fo.truncate()

    #Trytoreadfilenow

    line=fo.readline()

    print"ReadLine:%s"%(line)

    #Closeopendfile

    fo.close()

    當我們運行上面的程序,它會產生以下結果:

    Nameofthefile:foo.txt

    ReadLine:Thisis1stline

    ReadLine:

    以上就是pythontruncate方法的介紹,在裁剪文件方面經常會使用到,可以就這種方法先進行一個練習。更多Python學習教程請關注IT培訓機構:千鋒教育。

    聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。

    猜你喜歡LIKE

    python delattr函數如何使用?

    2023-11-10

    python time.strptime的格式化

    2023-11-10

    pythonGIL在Python多線程的應用

    2023-11-10

    最新文章NEW

    python中pdb模塊怎么用?

    2023-11-10

    Python如何截圖保存?

    2023-11-10

    python?中缺少module怎么辦?

    2023-11-10

    相關推薦HOT

    更多>>

    快速通道 更多>>

    最新開班信息 更多>>

    網友熱搜 更多>>