OS File 中的 os.ftruncate(fd, length

首页 / Python2入门教程 / OS File 中的 os.ftruncate(fd, length

Python方法ftruncate()截断文件描述符fd对应的文件,使其最大长度为字节。

os.ftruncate(fd, length) - 语法

os.ftruncate(fd, length)
  • fd         -  这是需要截断的文件描述符。

  • length  -  这是需要截断文件的长度。

os.ftruncate(fd, length) - 示例

以下示例显示ftruncate()方法的用法。

无涯教程网

#!/usr/bin/python

import os, sys

# Open a file
fd=os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Write one string
os.write(fd, "This is test - This is test")

# Now you can use ftruncate() method.
os.ftruncate(fd, 10)

# Now read this file from the beginning.
os.lseek(fd, 0, 0)
str=os.read(fd, 100)
print "Read String is : ", str

# Close opened file
os.close( fd )

print "Closed the file successfully!!"

当无涯教程运行上面的程序时,它产生以下输出-

Read String is :  This is te
Closed the file successfully!!

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

深入浅出gRPC -〔李林锋〕

从0开始学大数据 -〔李智慧〕

Web协议详解与抓包实战 -〔陶辉〕

数据中台实战课 -〔郭忆〕

Redis核心技术与实战 -〔蒋德钧〕

手机摄影 -〔@随你们去〕

手把手带你写一门编程语言 -〔宫文学〕

人人都用得上的数字化思维课 -〔付晓岩〕

AI大模型系统实战 -〔Tyler〕

好记忆不如烂笔头。留下您的足迹吧 :)