如何使用Python在Linux中查找所有文件的索引节点? [英] How to find the inode of all files in Linux with Python?

查看:87
本文介绍了如何使用Python在Linux中查找所有文件的索引节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Now get  the touple
info = os.fstat(fd)

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

# Close opened file
os.close( fd)

推荐答案

os.stat 函数返回有关文件的目录信息. inode 存储在 st_ino 字段中.这是一些入门的代码:

The os.stat function returns directory information about a file. The inode is stored in st_ino field. Here's some code to get you started:

>>> import glob
>>> import os
>>> for filename in glob.glob('*.py'):
        print(os.stat(filename).st_ino, filename)

要获取文件大小,请使用 st_size 字段:

To get the filesize, use the st_size field:

>>> os.stat(filename).st_size       
1481

要获取文件名,只需打印 filename 变量:

To get the filename, just print the filename variable:

>>> print(filename)
'hello.py'

这篇关于如何使用Python在Linux中查找所有文件的索引节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆