O módulo OS em Python fornece funções para interagir com o sistema operacional. O sistema operacional está incluído nos módulos de utilitário padrão do Python. Este módulo fornece uma maneira portátil de usar a funcionalidade dependente do sistema operacional.
os.ftruncate()método trunca o arquivo correspondente ao descritor de arquivo fd , de modo que tenha no máximo bytes de comprimento.

Sintaxe: os.ftruncate (fd, comprimento)

Parâmetros:
fd: Este é o descritor de arquivo que deve ser truncado.
comprimento: Este é o comprimento do arquivo até o qual o arquivo deve ser truncado.

Valor de retorno: este método não retorna nenhum valor.

Exemplo # 1:
Usando o os.ftruncate()método para truncar um arquivo

# 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)
# 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)