how to use python os path module

Namaste Friends,




Today, I am going to show you how to use python os.path module? or you can os.path module complete tutorial!


Let's start with some basic queries.


Os module Overview


The OS python module provides a simple and effective way of using current operating system local and dependent functionality.

In Simple words, This module works like an interface between the user and operating system that python is running on - be that Linux, Windows, Mac or Any Other.


Os Path Module Overview


This Sub-Module Provide us some extra features for handling computer path related functions. Yes! This Sub-Module also plays important role in some situations.

So, here I am going to show you os path module examples in simplest form.

So, let's start our tutorial.

Python os.path tutorial.



Untitled slides <!-- General and theme style sheets
In [1]:
#!/usr/bin/python

# ============================================
#       PYTHON OS.Path MODULE TUTORIAL
# ============================================
#
# Author : SSB
#          surajsinghbisht054@gmail.com
#          https://bitforestinfo.blogspot.com
# 
#
# Here, I am Using
# 
# Operating System : Ubuntu 16.04 LTS
# Python Version   : 2.7.12
# Editor           : ipython notebook
#
#
In [2]:
#
# Import Module
#
import os
In [3]:
# 
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.abspath(path)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Get absolutized version of the pathname path.
#
print os.listdir('.')
print os.path.abspath("test")
['.ipynb_checkpoints', 'test1', 'test', 'Untitled.ipynb']
/home/hackwithssb/Desktop/Blog/Tutorial_series/os/test
In [4]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.basename(path)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# 
# Return the base name of pathname path
#
#
path = "/home/hackwithssb/Desktop/Blog/Tutorial_series/os/test/file.txt"
print os.path.basename(path)
file.txt
In [5]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.dirname(path)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Return the directory name of pathname path
#
print path
print os.path.dirname(path)
/home/hackwithssb/Desktop/Blog/Tutorial_series/os/test/file.txt
/home/hackwithssb/Desktop/Blog/Tutorial_series/os/test
In [6]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.exists(path)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Return True if path exists.
#
print path
print os.path.exists(path)
print os.path.exists(os.path.dirname(path))
/home/hackwithssb/Desktop/Blog/Tutorial_series/os/test/file.txt
False
True
In [7]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.commonprefix(path_list)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Returns A Single String Path That Represents 
# A Common Prefix Present In All Of The Paths List.
#
#
paths_list = ['/one/two/three/four',
         '/one/two/three/five',
         '/one/two/three/six',
         '/one/two,three/seven',
         ]

print os.path.commonprefix(paths_list)
/one/two
In [8]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.getatime(path)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Return the time of last access of input path.
#
import time

print os.path.getatime("test/")

t = os.path.getatime("test/")

print time.ctime(t)
1492597596.06
Wed Apr 19 15:56:36 2017
In [9]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.getmtime(path)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Return the time of last modification of path.
#
import time

print os.path.getmtime("test/")

t = os.path.getmtime("test/")

print time.ctime(t)
1492596332.3
Wed Apr 19 15:35:32 2017
In [10]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.getctime(path)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Return the time of creation of path
#
#
import time

print os.path.getctime("test/")

t = os.path.getctime("test/")

print time.ctime(t)
1492596332.3
Wed Apr 19 15:35:32 2017
In [11]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.getsize(path)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Return the size, In bytes, of path.
#
#
print os.path.getsize("test/"), " bytes"
4096  bytes
In [12]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.isabs(path)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Return True if path is an absolute pathname.
#
print os.path.isabs("test")
print os.path.isabs(os.path.abspath("test"))
False
True
In [13]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.isfile(path)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# 
# Return True if path is an existing regular file.
#
print os.path.isfile("test")

print os.path.isfile("test/test_file1.pl")
False
True
In [14]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.isdir(path)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Return True if path is an existing directory.
#
print os.path.isdir("test/test_file.c")

print os.path.isdir("test")
False
True
In [15]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.join(path, *paths)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Join one or more path components intelligently.
#
print os.path.join("one","two")
print os.path.join("one:","two","three","four")
print os.path.join("one:/","two","three","four","infinite")
one/two
one:/two/three/four
one:/two/three/four/infinite
In [16]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.normpath(path)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Normalize a pathname by collapsing 
# redundant separators and up-level references
#
print os.path.normpath("path/././k")
print os.path.normpath("path//k")
print os.path.normpath("path/../..//k")
path/k
path/k
../k
In [17]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.realpath(path)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Return the canonical path of the specified filename
#
print os.path.realpath("./test_file.txt")
print os.path.realpath("../test_file.txt")
/home/hackwithssb/Desktop/Blog/Tutorial_series/os/test_file.txt
/home/hackwithssb/Desktop/Blog/Tutorial_series/test_file.txt
In [18]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.relpath(path[, start])
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Return a relative filepath to path either 
# from the current directory or from an optional 
# start directory.
#
#
print os.path.relpath("/home/../test_file")
print os.path.relpath("/test_file.txt")
../../../../../../test_file
../../../../../../test_file.txt
In [19]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.samefile(path1, path2)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Return True if both pathname arguments refer to 
# the same file or directory.
#
path_1 = "/home/hackwithssb/Desktop/Blog/Tutorial_series/os/test/test_file1.pl"
path_2 = "/home/hackwithssb/Desktop/Blog/Tutorial_series/os/test/test_file1.pl"
print os.path.samefile(path_1, path_2)
True
In [20]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.split(path)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Split the pathname path into a pair(head, tail) 
# where tail is filename and head is everything 
# leading up to that.
#
print os.path.split(path_1)
print os.path.split(path)
('/home/hackwithssb/Desktop/Blog/Tutorial_series/os/test', 'test_file1.pl')
('/home/hackwithssb/Desktop/Blog/Tutorial_series/os/test', 'file.txt')
In [21]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.splitdrive(path)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Split the pathname path into a pair (drive, tail) 
# where drive is either a drive specification or the empty string.
# Works on windows. not in linux
#
print os.path.splitdrive(path_1)
('', '/home/hackwithssb/Desktop/Blog/Tutorial_series/os/test/test_file1.pl')
In [22]:
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# os.path.splitext(path)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Split the pathname path into a pair (root, ext)
# where root = path
# and ext = file extension
#
print os.path.splitext(path_1)
('/home/hackwithssb/Desktop/Blog/Tutorial_series/os/test/test_file1', '.pl')
In [ ]:
 
-->


Thanks for Reading
If you like my blog
then don't forget to subscribe,
comment and share.

good day!

Share this

Related Posts

Previous
Next Post »