#!/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)
Asked
Active
Viewed 6,965 times
-1

Raymond Hettinger
- 216,523
- 63
- 388
- 485

seyyed
- 15
- 1
- 2
-
"All files" means what? You want to trawl the entire filesystem? A specific directory? Something else? (And do you have a particularly good reason to want to use Python? A subprocess invoking `find / -printf '%i\t%P\0'` might well be faster to run, and the output will be trivial to unambiguously parse). – Charles Duffy Feb 07 '18 at 05:22
-
1What specific part of what you want differs from what you have? Do you know how to retrieve stat data, but not where the inode is in that? Do you know how to retrieve stat data for a single file, but not how to recurse? If you have *both* those questions, they should probably be asked separately (or, even better, searched for separately, as each almost certainly exists in the knowledgebase individually) – Charles Duffy Feb 07 '18 at 05:28
-
yes i want get list of all files system with inode , name ,size and ... – seyyed Feb 07 '18 at 05:39
-
very poor question, with a piece of code not matching the question and no further text.. – Daniel Alder Sep 19 '22 at 23:55
1 Answers
6
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)
To get the filesize, use the st_size field:
>>> os.stat(filename).st_size
1481
To get the filename, just print the filename variable:
>>> print(filename)
'hello.py'

Raymond Hettinger
- 216,523
- 63
- 388
- 485
-
I'd read "all" to mean, well, *all* -- ie. that they're looking for a solution that recurses into subdirectories a la `os.walk()`. – Charles Duffy Feb 07 '18 at 05:25
-