Python os.fstat() 方法

Python File(檔) 方法 Python OS 檔/目錄方法


概述

os.fstat() 方法用於返回檔描述符fd的狀態,類似 stat()。

Unix,Windows上可用。

fstat 方法返回的結構:

  • st_dev: 設備資訊

  • st_ino: 檔的i-node值

  • st_mode: 檔資訊的掩碼,包含了檔的許可權資訊,檔的類型資訊(是普通檔還是管道檔,或者是其他的檔類型)

  • st_nlink: 硬連接數

  • st_uid: 用戶ID

  • st_gid: 用戶組 ID

  • st_rdev: 設備 ID (如果指定檔)

  • st_size: 檔大小,以byte為單位

  • st_blksize: 系統 I/O 塊大小

  • st_blocks: 檔的是由多少個 512 byte 的塊構成的

  • st_atime: 檔最近的訪問時間

  • st_mtime: 檔最近的修改時間

  • st_ctime: 檔狀態資訊的修改時間(不是檔內容的修改時間)

語法

fstat()方法語法格式如下:

os.fstat(fd)

參數

  • fd -- 檔的描述符。

返回值

返回檔描述符fd的狀態。

實例

以下實例演示了 fstat() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 打開檔

fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# 獲取元組
info = os.fstat(fd)

print "檔資訊 :", info

# 獲取檔 uid
print "檔 UID :%d" % info.st_uid

# 獲取檔 gid
print "檔 GID  :%d" % info.st_gid

# 關閉檔

os.close( fd)

執行以上程式輸出結果為:

檔資訊 : (33261, 3753776L, 103L, 1, 0, 0,
            102L, 1238783197, 1238786767, 1238786767)
檔 UID :0
檔 GID :0

Python File(檔) 方法 Python OS 檔/目錄方法