如何使用 Python 檢查檔案的許可權?


Python 中的檔案許可權使您能夠確定誰可以對檔案執行某些操作。這些檔案屬性包括讀取、寫入和執行許可權。Python 中的 os 模組,特別是 os 模組中的 os.chmod() 函式,用於設定或修改許可權。屬於 os 模組的 os.stat() 函式可用於檢查檔案的當前許可權。在 Python 程式中,管理和操作檔案許可權對於安全和訪問控制非常重要且至關重要。

在 Python 中檢查檔案許可權對於確保儲存在檔案中的資料的安全性和完整性非常重要。

  • 檔案許可權確定並決定誰可以訪問、修改或執行計算機系統中的檔案。

  • 透過在 Python 中檢查檔案許可權,您可以控制和限制對敏感檔案的訪問,阻止未經授權的使用者檢視或修改它們。

  • 它有助於防止意外或故意篡改關鍵檔案,保護資料的完整性和機密性。

  • 檔案許可權檢查對於執行使用者級安全性至關重要;因為不同的使用者可能對檔案具有不同的訪問級別。

  • 在處理敏感和機密資訊(例如個人或財務資料)時,驗證檔案許可權尤其重要。

  • 它允許您實施訪問控制,並且僅向受信任的個人或授權的程序授予許可權。

  • 透過自動檢查檔案許可權,您可以檢測對許可權本身進行的任何不一致或未經授權的入侵和更改。

  • 此檔案許可權檢查過程有助於提高應用程式或系統的整體安全配置檔案,同時最大程度地降低資料洩露或未經授權訪問的風險。

要檢查 Python 中檔案的許可權,您可以匯入並使用 os 模組,如上所述。此 os 模組非常有用,並提供多個與作業系統互動的函式。具體來說,您可以利用 os 模組的 os.access() 函式根據其許可權確定檔案可訪問性的範圍。

使用 os.access() 檢查檔案的許可權

示例

在此程式碼示例中,我們定義了 check_file_permissions() 函式,該函式以 file_path 引數作為引數。我們使用 os 模組的 os.access() 函式來檢查指定路徑下檔案的許可權。os.R_OK、os.W_OK 和 os.X_OK 常量分別表示讀取、寫入和執行許可權。

此函式分別檢查每個許可權,然後打印出一條訊息,明確說明是否為給定檔案授予許可權。

透過執行和執行此程式碼,將看到您可以輕鬆地在 Python 中找到檔案的許可權。

import os
def check_file_permissions(file_path):
   if os.access(file_path, os.R_OK):
      print(f"Read permission is granted for file: {file_path}")
   else:
      print(f"Read permission is not granted for file: {file_path}")
    
   if os.access(file_path, os.W_OK):
      print(f"Write permission is granted for file: {file_path}")

   else:
      print(f"Write permission is not granted for file: {file_path}")
    
   if os.access(file_path, os.X_OK):
      print(f"Execute permission is granted for file: {file_path}")
   else:

      print(f"Execute permission is not granted for file: {file_path}")
# Example usage
file_path = "path/to/file.txt"
check_file_permissions(file_path)

輸出

對於某些 file1.txt 檔案,輸出如下

Read permission is granted for file: /file1.txt
Write permission is granted for file: /file1.txt
Execute permission is not granted for file: /file1.txt

使用 Stat 模組

示例

首先,我們匯入 os 和 stat 模組。在此示例中,我們使用 os.stat() 函式獲取檔案的狀態資訊,包括檔案模式。檔案模式表示檔案的許可權。然後,我們使用 stat 模組中的常量(例如,stat.S_IRUSR 用於所有者的讀取許可權)進行按位運算,以檢查和驗證各個檔案許可權。

import os
import stat

def check_file_permissions(file_path):
   file_stat = os.stat(file_path)
   file_mode = file_stat.st_mode

   if stat.S_IRUSR & file_mode:
      print(f"Read permission is granted for the owner of file: {file_path}")
   else:
      print(f"Read permission is not granted for the owner of file: {file_path}")

    if stat.S_IWUSR & file_mode:
        print(f"Write permission is granted for the owner of file: {file_path}")
    else:
        print(f"Write permission is not granted for the owner of file: {file_path}")

    if stat.S_IXUSR & file_mode:
        print(f"Execute permission is granted for the owner of file: {file_path}")
    else:
        print(f"Execute permission is not granted for the owner of file: {file_path}")

# Example usage
file_path = "path/to/file.txt"
check_file_permissions(file_path)

輸出

對於某些 file1.txt 檔案,輸出如下

Read permission is granted for the owner of file: /file1.txt
Write permission is granted for the owner of file: /file1.txt
Execute permission is not granted for the owner of file: /file1.txt

使用 pathlib 模組

在這裡,我們利用並使用 pathlib 模組的 Path 類來建立一個指向檔案的 Path 物件。我們還匯入了 os 模組。然後,我們可以使用 os 模組的 access() 函式來檢查檔案的許可權。

示例

import os
from pathlib import Path

def check_file_permissions(file_path):
   file = Path(file_path)

   if file.is_file():
      if os.access(file_path, os.R_OK):
         print(f"Read permission is granted for file: {file_path}")
      else:
         print(f"Read permission is not granted for file: {file_path}")

      if os.access(file_path, os.W_OK):
         print(f"Write permission is granted for file: {file_path}")
      else:
         print(f"Write permission is not granted for file: {file_path}")

      if os.access(file_path, os.X_OK):
         print(f"Execute permission is granted for file: {file_path}")
      else:
         print(f"Execute permission is not granted for file: {file_path}")
   else:
      print(f"The specified path does not point to a file: {file_path}")

# Example usage

file_path = "path/to/file.txt"
check_file_permissions(file_path)

輸出

對於某些 file1.txt 檔案,輸出如下

Read permission is granted for file: /file1.txt
Write permission is granted for file: /file1.txt
Execute permission is not granted for file: /file1.txt

使用 os.access() 函式

在此示例中,我們使用 os.access() 函式來檢查檔案許可權。它以檔案路徑和模式引數(例如,os.R_OK 用於讀取許可權)作為引數,如果授予指定的許可權,則返回 True,否則返回 False。

示例

import os
def check_file_permissions(file_path):
   if os.access(file_path, os.R_OK):
      print(f"Read permission is granted for file: {file_path}")
   else:
      print(f"Read permission is not granted for file: {file_path}")

   if os.access(file_path, os.W_OK):
      print(f"Write permission is granted for file: {file_path}")
   else:
      print(f"Write permission is not granted for file: {file_path}")

   if os.access(file_path, os.X_OK):
      print(f"Execute permission is granted for file: {file_path}")

   else:
      print(f"Execute permission is not granted for file: {file_path}")

# Example usage
file_path = "path/to/file.txt"
check_file_permissions(file_path)

輸出

對於某些 file1.txt 檔案,它給出以下輸出

Read permission is granted for file: /file1.txt
Write permission is granted for file: /file1.txt
Execute permission is not granted for file: /file1.txt

使用 os.path 模組

在此示例中,我們結合了 os.path 模組中的各種函式和方法來執行更全面的檢查。首先,我們使用 os.path.isfile() 檢查路徑是否指向檔案。然後,我們使用 os.path.exists() 和 os.path.getsize() 方法驗證檔案是否確實存在且不為空。最後,在滿足所有條件的情況下,我們使用 os.access() 檢查檔案許可權。

示例

import os
def check_file_permissions(file_path):
   if os.path.isfile(file_path):
      if os.path.exists(file_path) and 
os.path.getsize(file_path) > 0:
         print(f"File exists and is not empty: {file_path}")
         if os.access(file_path, os.R_OK):
            print(f"Read permission is granted for file: 
{file_path}")
         else:
                print(f"Read permission is not granted for file:
{file_path}")
      else:
         print(f"File does not exist or is empty: 
{file_path}")
   else:
      print(f"Path does not point to a file: {file_path}")

# Example usage
file_path = "path/to/file.txt"
check_file_permissions(file_path)

輸出

對於某些 file1.txt 檔案,輸出如下

File exists and is not empty: /file1.txt
Read permission is granted for file: /file1.txt    

在多使用者環境中,檢查 Python 中的檔案許可權非常重要,在多使用者環境中,多個個人或程序可能會與相同的檔案互動和協作。我們在本文中看到了檢查檔案許可權的不同策略和模組及其功能,以及執行相同操作的其他方法。

總之,透過正確檢查、管理和驗證檔案許可權,您可以提高 Python 應用程式和系統的整體可靠性和安全性。

更新於: 2023年7月17日

10K+ 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告