Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, how can I get the file system of a given file path

In python, given a directory or file path like /usr/local, I need to get the file system where its available. In some systems it could be / (root) itself and in some others it could be /usr.

I tried os.statvfs it doesnt help. Do I have to run the df command with the path name and extract the file system from the output? Is there a better solution?

Its for linux/unix platforms only.

Thanks

like image 341
Umapathy Avatar asked Sep 06 '25 02:09

Umapathy


1 Answers

Here is a slightly modified version of a recipe found here. os.path.realpath was added so symlinks are handled correctly.

import os
def getmount(path):        
    path = os.path.realpath(os.path.abspath(path))
    while path != os.path.sep:
        if os.path.ismount(path):
            return path
        path = os.path.abspath(os.path.join(path, os.pardir))
    return path
like image 128
unutbu Avatar answered Sep 07 '25 23:09

unutbu