📜  Python| os.ftruncate() 方法

📅  最后修改于: 2022-05-13 01:54:58.422000             🧑  作者: Mango

Python| os.ftruncate() 方法

Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。

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

该方法等价于os.truncate(fd, length)方法。

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

代码 #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 truncated
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的文件的新内容。

代码#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 truncated
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