• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
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.  pandas head – Return First n Rows from DataFrame
  • 2.  Get Tomorrow’s Date as Datetime in Python
  • 3.  math.degrees() Python – How to Convert Radians to Degrees in Python
  • 4.  How to Repeat a Function in Python
  • 5.  Sort Files by Date in Python
  • 6.  Using Selenium to Check if Element Exists in Python
  • 7.  Python Subtract Days from Date Using datetime timedelta() Function
  • 8.  Using Python to Count Even Numbers in List
  • 9.  Python cube root – Find Cube Root of Number With math.pow() Function
  • 10.  Perform Reverse Dictionary Lookup in 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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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