Python os.lstat() Method

Python File(文件) 方法Python OS File/Directory Methods


Overview

The os.lstat() method is used to return file information similar to stat(), but does not follow symbolic links. On some platforms, this is an alias for fstat, such as Windows.

Syntax

lstat()The method syntax is as follows:

os.lstat(path)

Parameters

  • path-- The file whose information is to be returned.

Return Value

Returns file information.

Example

The following example demonstrates the use of the lstat() method:

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

import os, sys

# 打开文件
path = "/var/www/html/foo.txt"
fd = os.open( path, os.O_RDWR|os.O_CREAT )

# 关闭打开的文件
os.close( fd )

# 获取元组
info = os.lstat(path)

print "文件信息 :", info

# 获取文件 uid
print "文件 UID  :%d" % info.st_uid

# 获取文件 gid
print "文件 GID :%d" % info.st_gid

The output of the above program is:

文件信息 : (33261, 3450178L, 103L, 1, 500, 500, 0L, 
             1238866944, 1238866944, 1238948312)
文件 UID :500
文件 GID :500

Python File(文件) 方法Python OS File/Directory Methods

Other Extensions