Posted by
Suraj Singh
on April 19, 2017 ·
219 mins read
hii readers,
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 : # surajsinghbisht054@gmail.com # https://www.bitforestinfo.com # # # Here, I am Using # # Operating System : Ubuntu 16.04 LTS # Python Version : 2.7.12 # Editor : ipython notebook # #
In [2]:
# # Import Module # importos
In [3]:
# # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # os.path.abspath(path) # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # # Get absolutized version of the pathname path. # printos.listdir('.') printos.path.abspath("test")
# # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # os.path.basename(path) # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # # Return the base name of pathname path # # path="/home/hackwithssb/Desktop/Blog/Tutorial_series/os/test/file.txt" printos.path.basename(path)
file.txt
In [5]:
# # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # os.path.dirname(path) # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # # Return the directory name of pathname path # printpath printos.path.dirname(path)
# # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # 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', ]
printos.path.commonprefix(paths_list)
/one/two
In [8]:
# # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # os.path.getatime(path) # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # # Return the time of last access of input path. # importtime
printos.path.getatime("test/")
t=os.path.getatime("test/")
printtime.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. # importtime
printos.path.getmtime("test/")
t=os.path.getmtime("test/")
printtime.ctime(t)
1492596332.3 Wed Apr 19 15:35:32 2017
In [10]:
# # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # os.path.getctime(path) # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # # Return the time of creation of path # # importtime
printos.path.getctime("test/")
t=os.path.getctime("test/")
printtime.ctime(t)
1492596332.3 Wed Apr 19 15:35:32 2017
In [11]:
# # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # os.path.getsize(path) # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # # Return the size, In bytes, of path. # # printos.path.getsize("test/")," bytes"
4096 bytes
In [12]:
# # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # os.path.isabs(path) # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # # Return True if path is an absolute pathname. # printos.path.isabs("test") printos.path.isabs(os.path.abspath("test"))
False True
In [13]:
# # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # os.path.isfile(path) # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # # Return True if path is an existing regular file. # printos.path.isfile("test")
printos.path.isfile("test/test_file1.pl")
False True
In [14]:
# # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # os.path.isdir(path) # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # # Return True if path is an existing directory. # printos.path.isdir("test/test_file.c")
printos.path.isdir("test")
False True
In [15]:
# # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # os.path.join(path, *paths) # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # # Join one or more path components intelligently. # printos.path.join("one","two") printos.path.join("one:","two","three","four") printos.path.join("one:/","two","three","four","infinite")
# # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # os.path.relpath(path[, start]) # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # # Return a relative filepath to path either # from the current directory or from an optional # start directory. # # printos.path.relpath("/home/../test_file") printos.path.relpath("/test_file.txt")
# # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # 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" printos.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. # printos.path.split(path_1) printos.path.split(path)
# # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # 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 # printos.path.splitdrive(path_1)