# Directory name from path of file import os file_name = "/home/ubuntu/Project/demo.txt" dir_name = os.path.dirname(file_name) print(dir_name)