The Wayback Machine - https://web.archive.org/web/20240803072434/https://www.geeksforgeeks.org/os-path-module-python/
Open In App

OS Path module in Python

Last Updated : 23 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

OS Path module contains some useful functions on pathnames. The path parameters are either strings or bytes. These functions here are used for different purposes such as for merging, normalizing, and retrieving path names in Python. All of these functions accept either only bytes or only string objects as their parameters. The result is an object of the same type if a path or file name is returned. As there are different versions of the operating system there are several versions of this module in the standard library. In this article, we will start working with Paths in the Python OS Module.

Working with Paths in Python OS Module

Below are some OS Path functions by which we can do Path Manipulation in the Python OS Module:

  • os.path.basename(path)
  • os.path.dirname(path)
  • os.path.isabs(path)
  • os.path.isdir(path)
  • os.path.isfile(path)
  • os.path.normcase(path)
  • os.path.normpath(path)

os.path.basename(path)

os.path.basename(path) is used to return the basename of the file. This function returns the file name from the path given.

Python3




# basename function
import os
out = os.path.basename("/baz/foo")
print(out)


Output

foo

os.path.dirname(path)

os.path.dirname(path) is used to return the directory name from the path given. This function returns the name from the path except the path name. 

Python3




# dirname function
import os
out = os.path.dirname("/baz/foo")
print(out)


Output

/baz

os.path.isabs(path)

os.path.isabs(path) specifies whether the path is absolute or not. In Unix system absolute path means path begins with the slash(‘/’) and in Windows that it begins with a (back)slash after chopping off a potential drive letter. 

Python




# isabs function
import os
out = os.path.isabs("/baz/foo")
print(out)


Output

True

os.path.isdir(path)

os.path.isdir(path) function specifies whether the path is existing directory or not. 

Python




# isdir function
import os
out = os.path.isdir("C:\\Users")
print(out)


Output:

True

os.path.isfile(path)

os.path.isfile(path) function specifies whether the path is existing file or not. 

Python




# isfile function
import os
out = os.path.isfile("C:\\Users\foo.csv")
print(out)


Output: 

True

os.path.normcase(path)

os.path.normcase(path) function normalizes the case of the pathname specified. In Unix and Mac OS X system it returns the pathname as it is . But in Windows it converts the path to lowercase and forward slashes to backslashes. 

Python




# normcase function in windows
import os
out = os.path.normcase("/BAz")
print(out)


Output:

'\\baz'

os.path.normpath(path)

os.path.normpath(path) function normalizes the path names by collapsing redundant separators and up-level references so that A//B, A/B/, A/./B and A/foo/../B all become A/B. On Windows, it converts forward slashes to backward slashes . 

Python




# normpath function in Unix
import os
out = os.path.normpath("foo/./bar")
print(out)


Output: 

'foo/bar'


Similar Reads

How to import a Python module given the full path?
The Python module is a file consisting of Python code with a set of functions, classes, and variable definitions. The module makes the code reusable and easy to understand. The program that needs to use the module should import that particular module. In this article, we will discuss how to import a Python module given its full path. Different Ways
5 min read
Find Shortest Path Using Python OSMnx Routing Module
This article demonstrates how to use the OSMnx routing module to find the shortest path, illustrated with practical examples. Syntax of osmnx.routing.shortest_path() FunctionOSMnx routing module has the 'shortest_path' functionality to find the nearest path from the origin node to the destination node. The function is as follows: osmnx.routing.shor
3 min read
Find K Shortest Path Using OSMnx Routing Module in Python
OSMnx, a Python library built on top of OpenStreetMap data, offers a powerful set of tools for street network analysis. One common task in network analysis is finding the K shortest paths between two locations. In this article, we'll explore how to find the k-shortest path using OSMnx module in Python. Syntax of osmnx.routing.k_shortest_paths() Fun
3 min read
MySQL-Connector-Python module in Python
MySQL is a Relational Database Management System (RDBMS) whereas the structured Query Language (SQL) is the language used for handling the RDBMS using commands i.e Creating, Inserting, Updating and Deleting the data from the databases. SQL commands are case insensitive i.e CREATE and create signify the same command. In this article, we will be disc
2 min read
twitter-text-python (ttp) module - Python
twitter-text-python is a Tweet parser and formatter for Python. Amongst many things, the tasks that can be performed by this module are : reply : The username of the handle to which the tweet is being replied to. users : All the usernames mentioned in the tweet. tags : All the hashtags mentioned in the tweet. urls : All the URLs mentioned in the tw
3 min read
Python calendar module : formatmonth() method
Calendar module allows to output calendars like program, and provides additional useful functions related to the calendar. Functions and classes defined in Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions. class calendar.TextCalendar(firstweekday=0) can be used to generate plain text
2 min read
Python | Writing to an excel file using openpyxl module
Prerequisite : Reading an excel file using openpyxl Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The openpyxl module allows Python program to read and modify Excel files. For example, user might have to go through thousands of rows and pick out few handful information to make small changes b
3 min read
Stack and Queue in Python using queue Module
A simple python List can act as queue and stack as well. Queue mechanism is used widely and for many purposes in daily life. A queue follows FIFO rule(First In First Out) and is used in programming for sorting and for many more things. Python provides Class queue as a module which has to be generally created in languages such as C/C++ and Java. 1.
3 min read
PyMsgBox module in Python
PyMsgBox is simple, cross-platform, purely implemented in Python for message boxes as JavaScript has. It uses Python's built-in Tkinter module for its GUI. Installation This module does not built-in Python. To install it type the below command in the terminal. pip install PyMsgBox There are four functions in PyMsgBox, which follow JavaScript’s mess
2 min read
uniform() method in Python Random module
uniform() is a method specified in the random library in Python 3. Nowadays, in general, day-day tasks, there's always the need to generate random numbers in a range. Normal programming constructs require a method more than just one word to achieve this particular task. In python, there's an inbuilt method, "uniform()" which performs this task with
2 min read
three90RightbarBannerImg