• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / Python dirname – Get Directory Name of Path using os.path.dirname()

Python dirname – Get Directory Name of Path using os.path.dirname()

February 27, 2022 Leave a Comment

The Python dirname() function is very useful for getting the directory name of a given path.

import os

print(os.path.dirname("/Home/Documents/example.py")

#Output:
/Home/Documents

When working with file systems, it can be useful to be able to get the directory name and path of a file.

The Python os module provides us with a number of great functions to be able to perform many operating system tasks.

With the os module, we can easily get the directory name of a path.

To get the directory name of a path, we can use the os.path.dirname() function.

Pass a string representing the path of a file, and you will get the full path of the directory of that file.

Below is an example of how to use the Python dirname() function to get the directory name of a file.

import os

print(os.path.dirname("/Home/Documents/example.py")

#Output:
/Home/Documents

Hopefully this article has been useful for you to learn how to get the directory name of a path using the Python dirname() function.

Other Articles You'll Also Like:

  • 1.  PROC FREQ Equivalent in Python
  • 2.  Python atan2 – Find Arctangent of the Quotient of Two Numbers
  • 3.  Python Trig – Using Trigonometric Functions in Python for Trigonometry
  • 4.  Convert pandas Series to List in Python
  • 5.  Format Numbers as Dollars in Python with format()
  • 6.  Count Primes Python – How to Count Number of Primes in a List
  • 7.  pandas sum – Get Sum of Series or DataFrame Columns
  • 8.  Python ljust Function – Left Justify String Variable
  • 9.  Inverting Dictionary Variables in Python with Dictionary Comprehension
  • 10.  Remove Last Element from List Using Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy