Python os.lstat()方法

2019-10-16 23:04:40

Python的lseek()方法與fstat()非常相似,並返回一個包含有關檔案資訊的stat_result物件,但不要跟隨符號連結。這是在不支援符號連結的平台上的fstat()的別名,例如:Windows

這是lstat方法返回的結構 -

  • st_dev - 包含檔案的裝置的ID
  • st_ino - inode編號
  • st_mode - 保護
  • st_nlink - 硬連結數
  • st_uid - 所有者的使用者ID
  • st_gid - 所有者的組ID
  • st_rdev - 裝置ID(如果是特殊檔案)
  • st_size - 總大小(以位元組為單位)
  • st_blksize - 檔案系統I/O的塊大小
  • st_blocks - 分配的塊數
  • st_atime - 上次存取的時間
  • st_mtime - 上次修改的時間
  • st_ctime - 上次狀態更改的時間

語法

以下是lstat()方法的語法 -

os.lstat(path)

引數

  • path - 這是檔案描述符,需要處理。

定義的pos常數 -

  • os.SEEK_SET = 0
  • os.SEEK_CUR = 1
  • os.SEEK_END = 2

返回值

  • 這是要返回資訊的檔案。

範例

以下範例顯示了lstat()方法的用法。

#!/usr/bin/python3
import os, sys

# Open a file
path = "d:\\python3\\foo.txt"
fd = os.open( path, os.O_RDWR|os.O_CREAT )

# Close opened file
os.close( fd )

# Now get  the touple
info = os.lstat(path)

print ("File Info :", info)

# Now get uid of the file
print ("UID of the file :%d" % info.st_uid)

# Now get gid of the file
print ("GID of the file :%d" % info.st_gid)

執行上面程式碼後,將得到以下結果 -

File Info : os.stat_result(st_mode=33206, st_ino=281474976797706, st_dev=1017554828, st_nlink=2, st_uid=0, st_gid=0, st_size=13, st_atime=1455597777, st_mtime=1438077266, st_ctime=1455560006)
UID of the file :0
GID of the file :0