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


Python os.truncate()用法及代碼示例

Python中的OS模塊提供了與操作係統進行交互的函數。操作係統屬於Python的標準實用程序模塊。該模塊提供了使用依賴於操作係統的函數的便攜式方法。

os.truncate()Python中的方法用於將指定路徑指示的文件截斷為最大指定長度。

用法: os.truncate(path, length) 

參數:
path:代表文件係統路徑的path-like對象。這將指示文件被截斷。
path-like對象是表示路徑的字符串或字節對象。 length:一個整數值,表示文件將被截斷到的長度(以字節為單位)。

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

將以下文本視為名為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.truncate()方法的使用
# Python program to explain os.truncate() method  
    
# importing os module  
import os 
  
# File path 
path = "/home / ihritik / Desktop / Python_intro.txt"
  
# Print the original size of the file (in bytes) 
print("File size (in bytes):", os.path.getsize(path)) 
  
# Length (in Bytes) to which  
# the file will be trunctated 
length = 72
  
# Truncate the file  
# to at most given length 
# using os.truncate() method 
os.truncate(path, length) 
  
# Print the content of file 
print("Content of file Python_intro.txt:") 
with open(path, 'r') as f: 
    print(f.read())  
  
# Print the new size of the file (in bytes) 
print("File size (in bytes):", os.path.getsize(path))
輸出:
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.truncate() method  
    
# importing os module  
import os 
  
# File path 
path = "/home / ihritik / Desktop / Python_intro.txt"
  
# Print the original size of the file (in bytes) 
print("File size (in bytes):", os.path.getsize(path)) 
  
# Length (in Bytes) to which  
# the file will be trunctated 
length = 72
  
# Truncate the file  
# to at most given length 
# using os.truncate() method 
os.truncate(path, length) 
  
# Print the content of file 
print("Content of file Python_intro.txt:") 
with open(path, 'r') as f: 
    print(f.read())  
  
# Print the new size of the file (in bytes) 
print("File size (in bytes):", os.path.getsize(path))
輸出:
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.truncate()方法刪除文件內容
# Python program to explain os.truncate() method  
    
# importing os module  
import os 
  
# File path 
path = "/home / ihritik / Desktop / Python_intro.txt"
  
# Print the original size of the file (in bytes) 
print("File size (in bytes):", os.path.getsize(path)) 
  
# specify the length as 0 
# to delete the file content 
length = 0
  
# Truncate the file  
# to length 0 
os.truncate(path, length) 
  
# Print the content of file 
print("Content of file Python_intro.txt:") 
with open(path, 'r') as f: 
    print(f.read())  
  
# Print the new size of the file (in bytes) 
print("File size (in bytes):", os.path.getsize(path)) 
  
# Consider the same Python_intro.txt file 
# used in above example for this example
輸出:
File size (in bytes): 100
Content of file Python_intro.txt:

File size (in bytes): 0


相關用法


注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | os.truncate() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。