📜  Python| os.ftruncate() 方法

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

Python| os.ftruncate() 方法

Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
os.ftruncate()方法截断与文件描述符fd对应的文件,使其大小最多为 length 个字节。

示例 #1:
使用os.ftruncate()方法截断文件

# Python program to explain os.ftruncate() method 
        
# importing os module 
import os 
    
# path 
path = 'C:/Users/Rajnish/Desktop/testfile.txt'
  
# Open the file and get
# the file descriptor associated
# with it using os.open() method
fd = os.open(path, os.O_RDWR|os.O_CREAT)
  
# String to be written
s = 'GeeksforGeeks'
  
# Convert the string to bytes 
line = str.encode(s)
  
# Write the bytestring to the file 
# associated with the file 
# descriptor fd 
os.write(fd, line)
  
# Using os.ftruncate() method
os.ftruncate(fd, 5)
  
# Seek the file from beginning
# using os.lseek() method
os.lseek(fd, 0, 0)
  
# Read the file
s = os.read(fd, 15)
  
# Print string
print(s)
  
# Close the file descriptor 
os.close(fd)
输出:
b'Geeks'

示例 #2:
使用os.ftruncate()方法截断文件

# Python program to explain os.ftruncate() method 
        
# importing os module 
import os 
    
# path 
path = 'C:/Users/Rajnish/Desktop/testfile.txt'
  
# Open the file and get
# the file descriptor associated
# with it using os.open() method
fd = os.open(path, os.O_RDWR|os.O_CREAT)
  
# String to be written
s = 'GeeksforGeeks - Computer Science portal'
  
# Convert the string to bytes 
line = str.encode(s)
  
# Write the bytestring to the file 
# associated with the file 
# descriptor fd 
os.write(fd, line)
  
# Using os.ftruncate() method
os.ftruncate(fd, 10)
  
# Seek the file from beginning
# using os.lseek() method
os.lseek(fd, 0, 0)
  
# Read the file
s = os.read(fd, 15)
  
# Print string
print(s)
  
# Close the file descriptor 
os.close(fd)
输出:
b'GeeksforGe'