當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。