如何使用 Python 獲取檔案系統資訊?
在 Python 中,檔案系統資訊是指與檔案或目錄關聯的屬性和元資料,例如其名稱、大小、時間戳、許可權、所有權和型別。Python 中有各種模組,如 os 和 os.path,可用於處理檔案系統並獲取此資訊。訪問檔案系統資訊使開發人員能夠處理檔案和目錄,執行建立和刪除等操作,並在程式碼中做出明智的決策。
要使用 Python 獲取檔案系統資訊,您可以使用 os 模組;它提供了多個與作業系統互動的功能。特別是,os 模組中的 os.statvfs() 函式可用於檢索有關檔案系統的資訊。讓我們詳細探討如何逐步解釋和程式碼示例來使用此函式。
匯入所需的模組
import os
檢索檔案系統資訊
接下來,您可以繼續使用 os.statvfs() 函式來獲取檔案系統資訊。此函式接受檔案或目錄的路徑作為引數,並返回一個 os.statvfs_result 物件,該物件輸出表示檔案系統屬性的各種屬性。
# Specify the path to the file or directory path = '/path/to/file_or_directory' # Retrieve the file system information fs_info = os.statvfs(path)
訪問檔案系統屬性
獲得 os.statvfs_result 物件後,可以訪問不同的屬性以獲取特定的檔案系統資訊。一些常用的屬性如下所示
fs_favail − 檔案系統中可用檔案節點的數量。
fs_files − 檔案系統中檔案節點的總數。
f_bsize − 最佳檔案系統塊大小。
f_frsize − 基本檔案系統塊大小。
f_blocks − 檔案系統中塊的總數。
f_bfree − 檔案系統中空閒塊的數量。
示例
讓我們考慮一個演示訪問和列印某些屬性的示例−
# Print some file system information
print("File System Information:")
print("Available File Nodes:", fs_info.f_favail)
print("Total File Nodes:", fs_info.f_files)
print("Optimal Block Size:", fs_info.f_bsize)
如果以上程式碼和之前的程式碼作為一個程式碼段執行,對於某個檔案,我們可能會得到以下輸出
輸出
File System Information: Available File Nodes: 6859438 Total File Nodes: 7208960 Optimal Block Size: 4096
執行程式碼
程式碼被儲存為 Python 檔案並執行。您確保將“/path/to/file_or_directory”替換為您想要提取檔案系統資訊的實際檔案或目錄的路徑。
透過使用 os.statvfs() 函式並訪問返回物件的適當屬性,您可以收集有關相關檔案系統的寶貴見解。
獲取磁碟使用情況資訊
在此示例中,我們使用 shutil 模組的 disk_usage() 函式來獲取檔案系統的磁碟使用情況統計資訊。此函式輸出一個命名元組,該元組具有以位元組為單位的總磁碟空間、已用磁碟空間和可用磁碟空間等屬性。
示例
import shutil
# Specify the path to the file or directory
path = '/path/to/file_or_directory'
# Get disk usage information
usage = shutil.disk_usage(path)
# Print disk usage information
print("Disk Usage Information:")
print("Total Space:", usage.total)
print("Used Space:", usage.used)
print("Free Space:", usage.free)
輸出
如果執行以上程式碼,對於某個目錄,我們可能會得到以下輸出
Disk Usage Information: Total Space: 115658190848 Used Space: 26008670208 Free Space: 89632743424
使用 Psutil 庫
在這裡,我們瞭解了 psutil 庫;它提供了一種跨平臺的方式來提取系統資訊,包括與檔案系統相關的詳細資訊。接下來,我們使用 psutil.disk_usage() 函式來獲取磁碟使用情況統計資訊。
示例
import psutil
# Specify the path to the file or directory
path = '/path/to/file_or_directory'
# Get disk usage information
usage = psutil.disk_usage(path)
# Print disk usage information
print("Disk Usage Information:")
print("Total Space:", usage.total)
print("Used Space:", usage.used)
print("Free Space:", usage.free)
輸出
如果執行以上程式碼,對於某個檔案,我們可能會得到以下輸出
Disk Usage Information: Total Space: 115658190848 Used Space: 26009186304 Free Space: 89632227328
在這兩個示例中,我們都指定了要提取檔案系統資訊的文
您不要忘記將“/path/to/file_or_directory”替換為您要檢查的實際路徑。您可以執行這些程式碼示例以檢索檔案系統資訊,並深入瞭解指定位置的磁碟使用情況。
使用 Os 模組
在此程式碼示例中,我們使用 os 模組中的 os.statvfs() 函式;我們使用它來檢索檔案系統資訊。此函式提供有關與指定路徑關聯的檔案系統的詳細資訊,包括總大小、可用空間等等。
示例
在此程式碼中,我們使用 os.statvfs() 函式來獲取檔案系統統計資訊物件。然後,我們獲取物件的特定屬性來計算以位元組為單位的總空間和可用空間。最後,我們將檔案系統資訊列印到控制檯。
import os
# Specify the path to the file or directory
path = '/path/to/file_or_directory'
# Get file system information
stat = os.statvfs(path)
# Calculate the total and available space in bytes
total_space = stat.f_frsize * stat.f_blocks
available_space = stat.f_frsize * stat.f_bavail
# Print file system information
print("File System Information:")
print("Total Space:", total_space)
print("Available Space:", available_space)
輸出
如果執行以上程式碼,對於某個檔案,我們可能會得到以下輸出
File System Information: Total Space: 115658190848 Available Space: 89632002048
使用 Subprocess 模組
在這裡,我們使用 subprocess 模組來執行系統命令並捕獲或獲取檔案系統資訊的快照。此方法取決於執行 df 等命令列實用程式並解析輸出以提取相關資訊。
示例
在下面的程式碼中,我們使用 subprocess.check_output() 函式來執行和執行 df 命令並將輸出捕獲為字串。然後,我們將輸出拆分為行,並透過拆分和訪問特定元素來提取所需資訊。最後,我們在控制檯上顯示檔案系統資訊。
import subprocess
# Specify the path to the file or directory
path = '/path/to/file_or_directory'
# Execute the 'df' command and capture the output
command = ['df', path]
output = subprocess.check_output(command).decode('utf-8')
# Extract the file system information from the output
lines = output.strip().split('\n')
header = lines[0].split()
data = lines[1].split()
total_space = int(data[1])
available_space = int(data[3])
# Print file system information
print("File System Information:")
print("Total Space:", total_space)
print("Available Space:", available_space)
輸出
如果執行以上程式碼,對於某個檔案,我們可能會得到以下輸出
File System Information: Total Space: 112947452 Available Space: 87530992
簡而言之,我們已經看到了使用 Python 查詢檔案系統資訊的各種方法。除了討論的程式碼示例外,其他示例還提供了使用 Python 獲取檔案系統資訊的替代方法,使您能夠靈活地選擇最適合您的需求和用例的方法。
透過本文,您學習了實踐給定程式碼示例的動手方法,從而檢索檔案系統資訊並深入瞭解使用 Python 的任何指定位置的磁碟使用情況和可用性。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP