首页 > Python资料 博客日记

Python os.ftruncate()方法

2023-08-07 17:23:53Python资料围观177

本篇文章分享Python os.ftruncate()方法,对你有帮助的话记得收藏一下,看Python资料网收获更多编程知识

Python的os.ftruncate()方法截断与文件描述符fd相对应的文件,使其最多大小为length个字节。

语法

以下是ftruncate()方法的语法 -

os.ftruncate(fd, length)

参数

  • fd − 这是文件描述符,需要被截断。
  • length − 这是文件需要被截断的文件的长度。

返回值

  • 此方法不返回任何值,仅在Unix系统上可用。

示例

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

#!/usr/bin/python3
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!!

版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐