Number of free inodes on a partition containing a directory

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?

Answers


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.


Need Your Help

selenium2 webdriver can't find the element newly created by javascript

java webdriver selenium-webdriver

Click on a link triggers an operation that creates new element. However, calling driver.findElement(By.id("")) after click returns does not find it.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.