如何使用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

在這兩個示例中,我們都指定了要從中提取檔案系統資訊的資料夾或檔案的路徑。然後,我們使用路徑作為引數呼叫適當的函式(shutil.disk_usage()或psutil.disk_usage())以獲取磁碟使用情況資訊。最後,我們訪問返回物件的屬性並列印相關細節。

不要忘記將'/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的任何指定位置的磁碟使用情況和可用性。

更新於:2023年7月20日

1K+ 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.