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()方法的用法。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/python/os-ftruncate.html

来源:LearnFk无涯教程网

#!/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!!

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

技术教程推荐

深入浅出区块链 -〔陈浩〕

Java核心技术面试精讲 -〔杨晓峰〕

Java并发编程实战 -〔王宝令〕

重学线性代数 -〔朱维刚〕

陈天 · Rust 编程第一课 -〔陈天〕

零基础入门Spark -〔吴磊〕

郭东白的架构课 -〔郭东白〕

Serverless进阶实战课 -〔静远〕

大型Android系统重构实战 -〔黄俊彬〕

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