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

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

技术教程推荐

数据结构与算法之美 -〔王争〕

Nginx核心知识150讲 -〔陶辉〕

摄影入门课 -〔小麥〕

流程型组织15讲 -〔蒋伟良〕

说透芯片 -〔邵巍〕

如何落地业务建模 -〔徐昊〕

大数据经典论文解读 -〔徐文浩〕

去无方向的信 -〔小麥〕

手把手教你落地DDD -〔钟敬〕

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