find the root directory of a file in python

You can use the os.path module to find the root directory of a given file in Python. Here's an example:

main.py
import os

# define the file path
file_path = '/path/to/your/file.txt'

# get the root directory of the file's path
root_directory = os.path.abspath(os.path.join(file_path, os.pardir))

print(root_directory)
208 chars
10 lines

This code will print the absolute path of the root directory containing the file. The os.path.join() function is used to join the file's parent directory (identified using the os.pardir constant) with the file's name. The os.path.abspath() function is then used to get the absolute path of the resulting directory.

gistlibby LogSnag