Question

I have a Python script running under Linux that generates huge numbers of tiny files into a given directory. However, many Linux filesystems like ext4 have a fixed number of inodes set at creation time, so I want to make sure it's possible to save that many files into that directory before starting. From the command line, you can see this number using df -i /some/directory.

How do you find the number of free inodes on the filesystem that directory lives on, in Python?

Was it helpful?

Solution

This can be done using the statvfs system call. In Python (both 2 and 3), this can be accessed using os.statvfs. The call describes the filesystem containing the file/directory the path specifies.

So to get the number of free inodes, use

#import os
os.statvfs('/some/directory').f_favail

Also, it's possible that some percentage of the inodes are reserved for the root user. If the script is running as root and you want to allow it to use the reserved inodes, use f_ffree instead of f_favail.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top