Latest web development tutorials

Python os.ftruncate () metode

Python Berkas metode (File) Metode file / direktori Python OS


garis besar

os.ftruncate () file descriptor fd sesuai tanaman, tidak dapat melebihi ukuran file maksimum.

Tersedia di Unix.

tatabahasa

ftruncate () sintaks metode adalah sebagai berikut:

os.ftruncate(fd, length)¶

parameter

  • fd - file descriptor.

  • panjang - ukuran file yang ingin untuk memotong.

Kembali Nilai

Metode ini tidak memiliki nilai kembali.

contoh

Contoh berikut menunjukkan metode ftruncate () penggunaan:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 打开文件
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# 写入字符串
os.write(fd, "This is test - This is test")

# 使用 ftruncate() 方法
os.ftruncate(fd, 10)

# 读取内容
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print "读取的字符串是 : ", str

# 关闭文件
os.close( fd)

print "关闭文件成功!!"

Output program di atas adalah:

读取的字符串是 :  This is te
关闭文件成功!!

Python Berkas metode (File) Metode file / direktori Python OS