当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python os.ftruncate()用法及代码示例


Python中的OS模块提供了与操作系统进行交互的函数。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的函数的便携式方法。

os.ftruncate()Python中的方法用于将与指定文件描述符相对应的文件截断为指定长度。

此方法等效于os.truncate(fd, length)方法。


用法: os.ftruncate(fd, length) 

参数:
fd:表示要截断文件的文件描述符。
长度:一个整数值,表示文件将被截断到的长度(以字节为单位)。

返回类型:此方法不返回任何值。

将以下文本视为名为Python_intro.txt的文件的内容。

Python is a widely used general-purpose, high level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code. Python is a programming language that lets you work quickly and integrate systems more efficiently.

代码1:os.ftruncate()方法的使用
# Python program to explain os.ftruncate() method  
    
# importing os module  
import os 
  
  
# Open the file and get  
# the file descriptor associated  
# with it using os.open() method 
fd = os.open("Python_intro.txt", os.O_RDWR)  
  
  
# Print the original size of the file (in bytes) 
print("File size (in bytes):", os.stat(fd).st_size) 
  
  
# Length (in Bytes) to which  
# the file will be trunctated 
length = 72
  
# Truncate the file  
# to at most given length 
# using os.ftruncate() method 
os.ftruncate(fd, length) 
  
# Print the content of file 
size = os.stat(fd).st_size 
print(os.read(fd, size).decode("utf-8")) 
  
# Print the size of file (in bytes) 
print("File size (in bytes):", os.stat(fd).st_size)
输出:
File size (in bytes): 409
Content of file Python_intro.txt:
Python is a widely used general-purpose, high level programming language
File size (in bytes): 72

将以下文本视为名为Python_intro.txt的文件的新内容。

Python is a widely used general-purpose, high level programming language

代码2:如果指定的长度超过文件大小
# Python program to explain os.ftruncate() method  
    
# importing os module  
import os 
  
# Open the file and get  
# the file descriptor associated  
# with it using os.open() method 
fd = os.open("Python_intro.txt", os.O_RDWR)  
  
  
# Print the original size of the file (in bytes) 
print("File size (in bytes):", os.stat(fd).st_size) 
  
# Length (in Bytes) to which  
# the file will be trunctated 
length = 100
  
# Truncate the file  
# to at most given length 
# using os.ftruncate() method 
os.ftruncate(fd, length) 
  
# Print the content of file 
size = os.stat(fd).st_size 
print(os.read(fd, size).decode("utf-8")) 
  
# Print the size of file (in bytes) 
print("File size (in bytes):", os.stat(fd).st_size)
输出:
File size (in bytes): 72
Content of file Python_intro.txt:
Python is a widely used general-purpose, high level programming language

File size (in bytes): 100

截断文件后的实际文件内容大小为72字节至100字节:
Python_intro.txt
直到其原始大小的文件内容都没有改变,但是为了将文件大小增加到指定的大小,它填充了一些无效字符。

代码3:使用os.ftruncate()方法删除文件内容
# Python program to explain os.ftruncate() method  
    
# importing os module  
import os 
  
  
# Open the file and get  
# the file descriptor associated  
# with it using os.open() method 
fd = os.open("Python_intro.txt", os.O_RDWR)  
  
  
# Print the original size of file (in bytes) 
print("File size (in bytes):", os.stat(fd).st_size) 
  
# specify the length as 0 
# to delete the file content 
length = 0
  
# Truncate the file  
# to length 0 
# using os.ftruncate() method 
os.ftruncate(fd, length) 
  
# Print the content of file 
size = os.stat(fd).st_size 
print(os.read(fd, size).decode("utf-8")) 
  
# Print the size of file (in bytes) 
print("File size (in bytes):", os.stat(fd).st_size)
输出:
File size (in bytes): 100
Content of file Python_intro.txt:
File size (in bytes): 0


相关用法


注:本文由纯净天空筛选整理自ihritik大神的英文原创作品 Python | os.ftruncate() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。