{"id":1123,"date":"2023-06-10T15:57:00","date_gmt":"2023-06-10T10:27:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=1123"},"modified":"2024-03-01T17:16:14","modified_gmt":"2024-03-01T11:46:14","slug":"python-pathlib-module","status":"publish","type":"post","link":"https:\/\/geekpython.in\/python-pathlib-module","title":{"rendered":"High-level Path Operations Using pathlib Module In Python"},"content":{"rendered":"\n<p>The&nbsp;<code>pathlib<\/code>&nbsp;module is a part of Python&#8217;s standard library and allows us to interact with filesystem paths and work with files using various methods and properties on the&nbsp;<code>Path<\/code>&nbsp;object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-getting-started-with-pathlib\"><a href=\"https:\/\/geekpython.in\/python-pathlib-module#heading-getting-started-with-pathlib\"><\/a>Getting Started With pathlib<\/h2>\n\n\n\n<p>The most frequently used class of the&nbsp;<code>pathlib<\/code>&nbsp;module is&nbsp;<code>Path<\/code>. It is better to kick off with the&nbsp;<code>Path<\/code>&nbsp;class if we are using this module for the first time or not sure which class to use for our task.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Importing Path class from pathlib\nfrom pathlib import Path\n\n# Instantiating the Path\npath = Path(__file__)\nprint(path)<\/pre><\/div>\n\n\n\n<p>In the above example, first, we imported the&nbsp;<code>Path<\/code>&nbsp;class from the&nbsp;<code>pathlib<\/code>&nbsp;module and then instantiated the&nbsp;<code>Path<\/code>&nbsp;with&nbsp;<code>__file__<\/code>.<\/p>\n\n\n\n<p>This returns the absolute path to the current file,&nbsp;<code>main.py<\/code>, on which we are working.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">D:\\SACHIN\\Pycharm\\pathlib_module\\main.py<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code>Path<\/code>&nbsp;class instantiates the file&#8217;s concrete path for the operating system on which the user is working. Because we&#8217;re using Windows, we&#8217;ll get the following output if we print the type of&nbsp;<code>path<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">print(type(path))\n\n----------\n&lt;class 'pathlib.WindowsPath'&gt;<\/pre><\/div>\n\n\n\n<p>Before we get into the methods and properties of&nbsp;<code>Path<\/code>, it&#8217;s important to understand that the&nbsp;<code>Path<\/code>&nbsp;classes are divided into&nbsp;<strong><em>pure paths<\/em><\/strong>&nbsp;and&nbsp;<strong><em>concrete paths<\/em><\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1400\" height=\"1000\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/Path.png\" alt=\"Pure class classification\" class=\"wp-image-1161\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/Path.png 1400w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/Path-300x214.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/Path-1024x731.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/Path-768x549.png 768w\" sizes=\"auto, (max-width: 1400px) 100vw, 1400px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-pure-paths\"><a href=\"https:\/\/geekpython.in\/python-pathlib-module#heading-pure-paths\"><\/a>Pure Paths<\/h2>\n\n\n\n<p>Pure paths enable us to manipulate the file paths of another operating system, such as manipulating the Windows path on a Unix machine or vice versa without accessing the operating system.<\/p>\n\n\n\n<p>Pure paths only support computational operations and do not support I\/O operations such as reading, writing, or manipulating files.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-pathlibs-purepath\"><a href=\"https:\/\/geekpython.in\/python-pathlib-module#heading-pathlibs-purepath\"><\/a>Pathlib&#8217;s PurePath<\/h2>\n\n\n\n<p><code>PurePath<\/code>&nbsp;is a class that is used to perform various operations on the path object. Consider the example below, in which we instantiate the&nbsp;<code>PurePath()<\/code>&nbsp;class.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Importing PurePath class from pathlib\nfrom pathlib import PurePath\n\npath = PurePath('main.py')\nprint(path)\nprint(type(path))\n\n----------\nmain.py\n&lt;class 'pathlib.PureWindowsPath'&gt;<\/pre><\/div>\n\n\n\n<p>We got the&nbsp;<code>PureWindowsPath()<\/code>&nbsp;path when we ran the above code because we are on a Windows machine, if we were on a non-Windows machine, we would get the&nbsp;<code>PurePosixPath()<\/code>&nbsp;path.<\/p>\n\n\n\n<p>The&nbsp;<code>PurePath()<\/code>&nbsp;has two subclasses, which are as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>PureWindowsPath()<\/code><\/li>\n\n\n\n<li><code>PurePosixPath()<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-purewindowspath\"><a href=\"https:\/\/geekpython.in\/python-pathlib-module#heading-purewindowspath\"><\/a>PureWindowsPath<\/h3>\n\n\n\n<p>This subclass is implemented for Windows filesystem paths, as the name suggests.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Importing PureWindowsPath class from pathlib\nfrom pathlib import PureWindowsPath\n\n# Instantiating PureWindowsPath\npath = PureWindowsPath('main.py')\nprint(path)\nprint(type(path))\n\n----------\nmain.py\n&lt;class 'pathlib.PureWindowsPath'&gt;<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-pureposixpath\"><a href=\"https:\/\/geekpython.in\/python-pathlib-module#heading-pureposixpath\"><\/a>PurePosixPath<\/h3>\n\n\n\n<p>This subclass is used for non-Windows filesystem paths.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Importing PurePosixPath class from pathlib\nfrom pathlib import PurePosixPath\n\n# Instantiating PurePosixPath\npath = PurePosixPath('main.py')\nprint(path)\nprint(type(path))\n\n----------\nmain.py\n&lt;class 'pathlib.PurePosixPath'&gt;<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-purepath-methods-and-properties\"><a href=\"https:\/\/geekpython.in\/python-pathlib-module#heading-purepath-methods-and-properties\"><\/a>PurePath Methods And Properties<\/h2>\n\n\n\n<p><code>PurePath<\/code>&nbsp;provides several methods that allow us to perform various operations on filesystem paths.<\/p>\n\n\n\n<p><strong>Getting the drive name<\/strong><\/p>\n\n\n\n<p>The&nbsp;<code>PurePath.drive<\/code>&nbsp;can be used to extract the drive name from the specified path. We&#8217;ll get a string representing the drive name, or an empty string if no drives are present in the path.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import PurePath\n\n# Path having a drive name\ndrive = PurePath('D:\/SACHIN\/Pycharm\/test.py').drive\nprint(drive)\n\n# Path without a drive name\nno_drive = PurePath('\/SACHIN\/Pycharm\/test.py').drive\nprint(no_drive)\n\n----------\nD:<\/pre><\/div>\n\n\n\n<p>The first part of the code has a drive name in its path, which we got in the output, but the second part of the code did not, so we got an empty string.<\/p>\n\n\n\n<p><strong>Getting the root and stem<\/strong><\/p>\n\n\n\n<p>The&nbsp;<strong>root<\/strong>&nbsp;is the file path&#8217;s top-level directory, which we can access with&nbsp;<code>PurePath.root<\/code>, and the&nbsp;<strong>stem<\/strong>&nbsp;is the last component of the file path without the suffix, which we can access with&nbsp;<code>PurePath.stem<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import PureWindowsPath, PurePosixPath\n\n# Getting the root\nroot = PureWindowsPath('D:\/SACHIN\/Pycharm\/').root\nprint(root)\n\n# Getting the root from the Unix-like path\nu_root = PurePosixPath('\/SACHIN\/Pycharm\/').root\nprint(u_root)\n\n# Getting the stem\nstem = PureWindowsPath('D:\/SACHIN\/Pycharm\/test.py').stem\nprint(stem)\n\n----------\n\\\n\/\ntest<\/pre><\/div>\n\n\n\n<p><strong>Getting the ancestors of the path<\/strong><\/p>\n\n\n\n<p>The&nbsp;<code>PurePath.parents<\/code>&nbsp;can be used to access the logical ancestors of the path.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import PureWindowsPath\n\nancestor = PureWindowsPath('D:\/SACHIN\/Pycharm\/test.py')\nprint(ancestor.parents[0])\nprint(ancestor.parents[1])\nprint(ancestor.parents[-1])\n\n----------\nD:\\SACHIN\\Pycharm\nD:\\SACHIN\nD:\\<\/pre><\/div>\n\n\n\n<p>Using the slicing technique, we were able to access the path&#8217;s ancestors. Python 3.10 added support for slices and negative index values for&nbsp;<code>PurePath.parents<\/code>.<\/p>\n\n\n\n<p>We got the full path except for the file name when we used 0, one directory back when we used 1, and the beginning portion of the path when we used -1.<\/p>\n\n\n\n<p><strong>Getting the parent<\/strong><\/p>\n\n\n\n<p>The&nbsp;<code>PurePath.parent<\/code>&nbsp;allows us to access the logical parent of the path.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import PureWindowsPath\n\np = PureWindowsPath('D:\/SACHIN\/Pycharm\/test.py')\nprint(p.parent)\n\n----------\nD:\\SACHIN\\Pycharm<\/pre><\/div>\n\n\n\n<p>In the above example, the parent directory of&nbsp;<code>test.py<\/code>&nbsp;is&nbsp;<code>Pycharm\/<\/code>, the parent directory of&nbsp;<code>Pycharm\/<\/code>&nbsp;is&nbsp;<code>SACHIN\/<\/code>, and the parent directory of&nbsp;<code>SACHIN\/<\/code>&nbsp;is the drive&nbsp;<code>D:\/<\/code>, which contains all of these directories and files.<\/p>\n\n\n\n<p>That&#8217;s why we got this output&nbsp;<code>D:\\SACHIN\\Pycharm<\/code>.<\/p>\n\n\n\n<p><strong>Getting the name and suffix<\/strong><\/p>\n\n\n\n<p><code>PurePath.name<\/code>&nbsp;provides access to the name of the path&#8217;s final component, while&nbsp;<code>PurePath.suffix<\/code>&nbsp;provides access to the file extension of the final component. If the file has multiple extensions, we can get the list of file extensions with&nbsp;<code>PurePath.suffixes<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import PureWindowsPath\n\n# Accessing the name of the final component of the path\nfile_name = PureWindowsPath('D:\/SACHIN\/Pycharm\/test.py').name\nprint(file_name)\n\n# Accessing the suffix of the final component of the path\nfile_suffix = PureWindowsPath('D:\/SACHIN\/Pycharm\/test.py').suffix\nprint(file_suffix)\n\n----------\ntest.py\n.py<\/pre><\/div>\n\n\n\n<p>The last component of the path is&nbsp;<code>test.py<\/code>, and the extension is&nbsp;<code>.py<\/code>, which is what we got in the output.<\/p>\n\n\n\n<p>What if our&nbsp;<code>test.py<\/code>&nbsp;file has extensions like&nbsp;<code>test.py.zip<\/code>? If we want to extract both extensions, we can use&nbsp;<code>PurePath.suffixes<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Accessing the multiple suffix\nfile_suffixes = PureWindowsPath('test.py.zip').suffixes\nprint(file_suffixes)\n\n----------\n['.py', '.zip']<\/pre><\/div>\n\n\n\n<p><strong>Check if a path is absolute<\/strong><\/p>\n\n\n\n<p>The absolute path is one that has both a root and a drive(if the naming convention allows), and we can use the&nbsp;<code>PurePath.is_absolute()<\/code>&nbsp;method to determine whether or not a path is absolute. Returns a boolean value.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import PureWindowsPath, PurePosixPath\n\nprint(PureWindowsPath('D:\/SACHIN\/').is_absolute())\nTrue\n\nprint(PureWindowsPath('\/SACHIN\/').is_absolute())\nFalse\n\nprint(PurePosixPath('\/SACHIN\/').is_absolute())\nTrue\n\nprint(PurePosixPath('D:\/SACHIN\/').is_absolute())\nFalse<\/pre><\/div>\n\n\n\n<p>Looking at the first two PureWindowsPath cases, we first get&nbsp;<code>True<\/code>&nbsp;because the path has both a drive and a root, but then we get&nbsp;<code>False<\/code>&nbsp;because the path lacks a drive.<\/p>\n\n\n\n<p>In the PurePosixPath cases, we first got&nbsp;<code>True<\/code>&nbsp;even though the path did not have a drive name because non-Windows paths do not include drive names like Windows paths. But when we used the drive name in the path, we got&nbsp;<code>False<\/code>.<\/p>\n\n\n\n<p><strong>Combining paths<\/strong><\/p>\n\n\n\n<p><code>PurePath.joinpath()<\/code>&nbsp;allows us to concatenate the path with the argument passed to it.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import PurePath, PureWindowsPath, PurePosixPath\n\nprint(PurePath('D:\/SACHIN\/').joinpath('test.txt'))\nD:\\SACHIN\\test.txt\n\nprint(PurePath('D:\/SACHIN\/').joinpath(PureWindowsPath('test_dir', 'test.txt')))\nD:\\SACHIN\\test_dir\\test.txt\n\nprint(PurePosixPath('\/SACHIN\/').joinpath(PurePosixPath('test_dir', 'test.txt')))\n\/SACHIN\/test_dir\/test.txt<\/pre><\/div>\n\n\n\n<p><strong>Matching the path<\/strong><\/p>\n\n\n\n<p><code>PurePath.match()<\/code>&nbsp;takes a pattern and matches the path against the provided pattern(glob style pattern). When the path is matched, it returns&nbsp;<strong>True<\/strong>, otherwise, it returns&nbsp;<strong>False<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import PureWindowsPath\n\np = PureWindowsPath('D:\/SACHIN\/test.txt').match('*.py')\nprint(p)\n\np1 = PureWindowsPath('D:\/SACHIN\/test.py').match('*.py')\nprint(p1)\n\np2 = PureWindowsPath('D:\/SACHIN\/test\/test.py').match('test\/*.py')\nprint(p2)\n\n----------\nFalse\nTrue\nTrue<\/pre><\/div>\n\n\n\n<p>Depending on the platform we&#8217;re working on, pattern matching can be case-sensitive.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import PureWindowsPath, PurePosixPath\n\nprint(PurePosixPath('\/test\/test.py').match('*.Py'))\nprint(PureWindowsPath('\/test\/test.py').match('*.Py'))\n\n----------\nFalse\nTrue<\/pre><\/div>\n\n\n\n<p><strong>Changing the name<\/strong><\/p>\n\n\n\n<p><code>PurePath.with_name()<\/code>&nbsp;accepts a&nbsp;<code>name<\/code>&nbsp;argument and returns the new path with the changed file name.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import PureWindowsPath\n\nchg_name = PureWindowsPath('D:\/SACHIN\/test.txt').with_name('test.py')\nprint(chg_name)\n\n----------\nD:\\SACHIN\\test.py<\/pre><\/div>\n\n\n\n<p>If there is no name in the path, then we&#8217;ll get a&nbsp;<code>ValueError<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">no_name = PureWindowsPath('D:\/').with_name('test.py')\nprint(no_name)\n\n----------\nTraceback (most recent call last):\n  ....\n    raise ValueError(\"%r has an empty name\" % (self,))\nValueError: PureWindowsPath('D:\/') has an empty name<\/pre><\/div>\n\n\n\n<p><strong>Changing the stem<\/strong><\/p>\n\n\n\n<p>The&nbsp;<code>PurePath.with_stem()<\/code>&nbsp;method creates a new path with a different stem.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import PureWindowsPath\n\nchg_stem = PureWindowsPath('D:\/SACHIN\/example.py').with_stem('test')\nprint(chg_stem)\n\n----------\nD:\\SACHIN\\test.py<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code>ValueError<\/code>&nbsp;is thrown if the path does not have a name.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">no_name = PureWindowsPath('D:\/').with_stem('test')\nprint(no_name)\n\n----------\nTraceback (most recent call last):\n  ....\n    raise ValueError(\"%r has an empty name\" % (self,))\nValueError: PureWindowsPath('D:\/') has an empty name<\/pre><\/div>\n\n\n\n<p><strong>Changing the suffix<\/strong><\/p>\n\n\n\n<p>We can change the suffix using&nbsp;<code>PurePath.with_suffix()<\/code>. If the file name lacks a suffix, the provided suffix will be appended.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import PureWindowsPath\n\nsuf = PureWindowsPath('D:\/SACHIN\/test.py').with_suffix('.txt')\nprint(suf)\n\nno_suf = PureWindowsPath('D:\/SACHIN\/test').with_suffix('.py')\nprint(no_suf)\n\n----------\nD:\\SACHIN\\test.txt\nD:\\SACHIN\\test.py<\/pre><\/div>\n\n\n\n<p>What happens if we supply an empty string? The file&#8217;s suffix will be removed.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">empty_suf = PureWindowsPath('D:\/SACHIN\/test.py').with_suffix('')\nprint(empty_suf)\n\n----------\nD:\\SACHIN\\test<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-concrete-paths\"><a href=\"https:\/\/geekpython.in\/python-pathlib-module#heading-concrete-paths\"><\/a>Concrete Paths<\/h2>\n\n\n\n<p>Concrete paths perform computational operations in addition to I\/O operations on filesystem paths. Unlike pure paths, we could use concrete paths to perform operations such as reading the file, writing data to the file, and even interacting with the files.<\/p>\n\n\n\n<p>We can make system calls on path objects thanks to concrete paths. Concrete paths are subclasses of pure path classes, and there are three ways to instantiate concrete paths:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Path()<\/code><\/li>\n\n\n\n<li><code>WindowsPath()<\/code><\/li>\n\n\n\n<li><code>PosixPath()<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-pathlibs-path\"><a href=\"https:\/\/geekpython.in\/python-pathlib-module#heading-pathlibs-path\"><\/a>Pathlib&#8217;s Path<\/h2>\n\n\n\n<p>At the beginning of the article, we saw a glimpse of the&nbsp;<code>Path<\/code>&nbsp;class, which is a subclass of the&nbsp;<code>PurePath<\/code>&nbsp;class that represents the concrete path of the filesystem path.<\/p>\n\n\n\n<p>When we instantiate the&nbsp;<code>Path()<\/code>&nbsp;class, it generates either&nbsp;<code>PosixPath<\/code>&nbsp;or&nbsp;<code>WindowsPath<\/code>&nbsp;object, depending on the machine we&#8217;re working on.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Importing Path class from pathlib\nfrom pathlib import Path\n\n# Instantiating the Path\npath = Path('D:\/SACHIN\/Pycharm')\nprint(path)\nprint(type(path))\n\n----------\nD:\\SACHIN\\Pycharm\n&lt;class 'pathlib.WindowsPath'&gt;<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code>Path()<\/code>&nbsp;created a concrete Windows path because we&#8217;re on a Windows machine.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-posixpath\"><a href=\"https:\/\/geekpython.in\/python-pathlib-module#heading-posixpath\"><\/a>PosixPath<\/h3>\n\n\n\n<p><code>PosixPath<\/code>&nbsp;is a subclass of&nbsp;<code>PurePosixPath<\/code>&nbsp;and&nbsp;<code>Path<\/code>&nbsp;class that represents concrete non-Windows filesystem paths.<\/p>\n\n\n\n<p>Because&nbsp;<code>PosixPath<\/code>&nbsp;will make system calls, we can&#8217;t instantiate it on our machine because it&#8217;s running on Windows.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Importing PosixPath class from pathlib\nfrom pathlib import PosixPath\n\n# Instantiating PosixPath\npath = PosixPath('main.py')\nprint(path)\n\n----------\nTraceback (most recent call last):\n  ....\n    raise NotImplementedError(\"cannot instantiate %r on your system\"\nNotImplementedError: cannot instantiate 'PosixPath' on your system<\/pre><\/div>\n\n\n\n<p>We can only instantiate the class that corresponds to our system, for example, we can instantiate the&nbsp;<code>WindowsPath<\/code>&nbsp;class on Windows machines and the&nbsp;<code>PosixPath<\/code>&nbsp;class on POSIX-compliant machines.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-windowspath\"><a href=\"https:\/\/geekpython.in\/python-pathlib-module#heading-windowspath\"><\/a>WindowsPath<\/h3>\n\n\n\n<p><code>WindowsPath<\/code>&nbsp;is a subclass of&nbsp;<code>PureWindowsPath<\/code>&nbsp;and&nbsp;<code>Path<\/code>&nbsp;class that represents concrete Windows filesystem paths.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Importing WindowsPath class from pathlib\nfrom pathlib import WindowsPath\n\n# Instantiating WindowsPath\npath = WindowsPath('main.py')\nprint(path)\nprint(type(path))\n\n----------\nmain.py\n&lt;class 'pathlib.WindowsPath'&gt;<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-path-methods\"><a href=\"https:\/\/geekpython.in\/python-pathlib-module#heading-path-methods\"><\/a>Path Methods<\/h2>\n\n\n\n<p>The&nbsp;<code>Path<\/code>&nbsp;class provides several methods for performing I\/O operations on filesystem paths by interacting with the operating system.<\/p>\n\n\n\n<p><strong>Getting the current working directory and home directory<\/strong><\/p>\n\n\n\n<p>You may have used&nbsp;<code>os.getcwd()<\/code>&nbsp;to get the current working directory,&nbsp;<code>Path.cwd()<\/code>&nbsp;does the same thing, returning the new path object of the current working directory.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Importing Path class from pathlib\nfrom pathlib import Path\n\n# Getting the current working directory\npath = Path.cwd()\nprint(path)\n\n----------\nD:\\SACHIN\\Pycharm\\pathlib_module<\/pre><\/div>\n\n\n\n<p>We obtained the path to our current working file, and we can see that the path separator is a backslash(<code>\\<\/code>) because we are using the Windows operating system.<\/p>\n\n\n\n<p><code>Path.home()<\/code>&nbsp;returns the path to the user&#8217;s home directory. If the home directory cannot be resolved, a&nbsp;<code>RuntimeError<\/code>&nbsp;is thrown.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Importing Path class from pathlib\nfrom pathlib import Path\n\n# Getting the home directory\npath = Path.home()\nprint(path)\n\n----------\nC:\\Users\\SACHIN<\/pre><\/div>\n\n\n\n<p><strong>Accessing the components of the path<\/strong><\/p>\n\n\n\n<p>We&#8217;ve seen the&nbsp;<code>PurePath<\/code>&nbsp;properties that help us access the path&#8217;s components, since,&nbsp;<code>Path<\/code>&nbsp;is a subclass of&nbsp;<code>PurePath<\/code>, we can use those properties with the&nbsp;<code>Path<\/code>&nbsp;class as well.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Importing Path class from pathlib\nfrom pathlib import Path\n\n# Instantiating the path\npath = Path('D:\/SACHIN\/test.py')\n\n# Accessing the drive name\nprint(path.drive)\nD:\n\n# Accessing the root\nprint(path.root)\n\\\n\n# Accessing the name\nprint(path.name)\ntest.py\n\n# Accessing the stem\nprint(path.stem)\ntest\n\n# Accessing the suffix\nprint(path.suffix)\n.py\n\n# Accesing the parent\nprint(path.parent)\nD:\\SACHIN<\/pre><\/div>\n\n\n\n<p><strong>Iterating the directories<\/strong><\/p>\n\n\n\n<p>Using&nbsp;<code>Path.iterdir()<\/code>, we can get the path objects of the contents of the specified directory.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import Path\n\npath = Path('D:\/SACHIN\/Pycharm\/pathlib_module')\n\n# Iterating the pathlib_module directory\nfor files in path.iterdir():\n    print(files)\n\n----------\nD:\\SACHIN\\Pycharm\\pathlib_module\\.idea\nD:\\SACHIN\\Pycharm\\pathlib_module\\files\nD:\\SACHIN\\Pycharm\\pathlib_module\\main.py\nD:\\SACHIN\\Pycharm\\pathlib_module\\test.py<\/pre><\/div>\n\n\n\n<p>The path in the above code points to the&nbsp;<code>pathlib_module<\/code>&nbsp;directory, and we obtained the path objects of the directories and files contained within&nbsp;<code>pathlib_module<\/code>.<\/p>\n\n\n\n<p>Here is another example of the&nbsp;<code>.iterdir()<\/code>&nbsp;method.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">path = Path('files')\n\nfor files in path.iterdir():\n    print(files)\n\n----------\nfiles\\example.md\nfiles\\file.py\nfiles\\test.txt<\/pre><\/div>\n\n\n\n<p>We iterated through the contents of the&nbsp;<code>files<\/code>&nbsp;directory, which is located in the current working directory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-filesystem-modification\"><a href=\"https:\/\/geekpython.in\/python-pathlib-module#heading-filesystem-modification\"><\/a>Filesystem Modification<\/h3>\n\n\n\n<p><strong>Creating a directory<\/strong><\/p>\n\n\n\n<p><code>Path.mkdir()<\/code>&nbsp;creates a new directory at the specified path with the default&nbsp;<code>mode=0o777<\/code>, which means the directory is accessible to all users and groups and has read, write, and execute permissions.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import Path\n\n# Creating a new dir at the specified path\npath = Path('D:\/SACHIN\/Pycharm\/pathlib_module\/new_dir').mkdir(mode=0o777)<\/pre><\/div>\n\n\n\n<p>When we execute the above code, a new directory called&nbsp;<code>new_dir<\/code>&nbsp;is created in the&nbsp;<code>pathlib_module<\/code>&nbsp;directory.<\/p>\n\n\n\n<p>If the path already exists, we will receive a&nbsp;<code>FileExistsError<\/code>. If we run the above code again, we&#8217;ll get the following result.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'D:\\\\SACHIN\\\\Pycharm\\\\pathlib_module\\\\new_dir'<\/pre><\/div>\n\n\n\n<p>The path we specified already exists which is why the directory is not created. However,&nbsp;<code>.mkdir()<\/code>&nbsp;has an&nbsp;<code>exist_ok<\/code>&nbsp;parameter that, when set to&nbsp;<code>True<\/code>, ignores the error.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">path = Path('D:\/SACHIN\/Pycharm\/pathlib_module\/new_dir').mkdir(exist_ok=True)\nprint('Directory created.')\n\n----------\nDirectory created.<\/pre><\/div>\n\n\n\n<p>Note: The path&#8217;s final component should not be the existing non-directory file.<\/p>\n\n\n\n<p><strong>Creating a file<\/strong><\/p>\n\n\n\n<p><code>Path.touch()<\/code>&nbsp;allows us to create a file with&nbsp;<code>mode=0o666<\/code>&nbsp;at the specified path, indicating that the file has read and write permissions for all users and groups but no executable permission. The&nbsp;<code>exist_ok<\/code>&nbsp;parameter defaults to&nbsp;<code>True<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import Path\n\n# Creating a new file at the specified path\npath = Path('D:\/SACHIN\/Pycharm\/pathlib_module\/sample.txt').touch()<\/pre><\/div>\n\n\n\n<p>A file called&nbsp;<code>sample.txt<\/code>&nbsp;will be created. We&#8217;ll get the&nbsp;<code>FileExistError<\/code>&nbsp;if we set&nbsp;<code>exist_ok=False<\/code>&nbsp;and run the code again.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Creating a new file at the specified path\npath = Path('D:\/SACHIN\/Pycharm\/pathlib_module\/sample.txt').touch(exist_ok=False)\n\n----------\nFileExistsError: [Errno 17] File exists: 'D:\\\\SACHIN\\\\Pycharm\\\\pathlib_module\\\\sample.txt'<\/pre><\/div>\n\n\n\n<p><strong>Renaming the files and directories<\/strong><\/p>\n\n\n\n<p>Methods like&nbsp;<code>.with_name<\/code>&nbsp;and&nbsp;<code>.with_stem<\/code>&nbsp;enable us to rename the file name of the specified path. To rename the files and directories, we can also use&nbsp;<code>Path.rename()<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import Path\n\npath = Path('files')\n\n# Renaming the directory\npath.rename('docs')\nprint('Directory renamed successfully.')\n\n----------\nDirectory renamed successfully.<\/pre><\/div>\n\n\n\n<p>The directory&nbsp;<code>files<\/code>&nbsp;will be renamed to the&nbsp;<code>docs<\/code>. What happens if the target file or directory name already exists? The code will raise a&nbsp;<code>FileExistsError<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">path = Path('docs')\n\n# Renaming the directory\npath.rename('files')\nprint('Directory renamed successfully.')\n\n----------\nFileExistsError: [WinError 183] Cannot create a file when that file already exists: 'docs' -&gt; 'files'<\/pre><\/div>\n\n\n\n<p>The above code threw an error because the directory named&nbsp;<code>files<\/code>&nbsp;already exist in the project directory.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"201\" height=\"105\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/file_dir.png\" alt=\"Directory tree\" class=\"wp-image-1163\"\/><\/figure>\n\n\n\n<p><strong>Removing the directory<\/strong><\/p>\n\n\n\n<p><code>Path.rmdir()<\/code>&nbsp;deletes the directory specified in the path, but only if it is empty, otherwise, an&nbsp;<code>OSError<\/code>&nbsp;is raised.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import Path\n\n# Removing the directory at the specified path\npath = Path('D:\/SACHIN\/Pycharm\/pathlib_module\/files').rmdir()<\/pre><\/div>\n\n\n\n<p>If we attempt to remove a directory that does not exist, we will receive a&nbsp;<code>FileNotFoundError<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">path = Path('D:\/SACHIN\/Pycharm\/pathlib_module\/files').rmdir()\n\n----------\nFileNotFoundError: [WinError 2] The system cannot find the file specified: 'D:\\\\SACHIN\\\\Pycharm\\\\pathlib_module\\\\files'<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-reading-and-writing-operations\"><a href=\"https:\/\/geekpython.in\/python-pathlib-module#heading-reading-and-writing-operations\"><\/a>Reading and Writing Operations<\/h3>\n\n\n\n<p><code>Path<\/code>&nbsp;class provides several methods to perform reading and writing operations on the file. Assume we have a text file with some data and we want to read and write that data.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"772\" height=\"112\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/sample_file.png\" alt=\"Sample text file\" class=\"wp-image-1164\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/sample_file.png 772w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/sample_file-300x44.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/sample_file-768x111.png 768w\" sizes=\"auto, (max-width: 772px) 100vw, 772px\" \/><\/figure>\n\n\n\n<p><strong>Opening the file<\/strong><\/p>\n\n\n\n<p>Before reading or writing data to the file, the&nbsp;<code>Path<\/code>&nbsp;class provides a&nbsp;<code>.open()<\/code>&nbsp;method that opens the file specified by the path. You may have already used the built-in&nbsp;<code>open()<\/code>, this method works in the same way.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import Path\n\n# Instantiating the path of the file\nopen_file = Path('sample_file.txt')\n\n# Using the open() method\nwith open_file.open(mode='r') as file:\n    # Reading the content\n    print(file.read())\n\n----------\nHi, I am sample file for testing.<\/pre><\/div>\n\n\n\n<p><strong>Reading the file<\/strong><\/p>\n\n\n\n<p>To read the content of the file specified by the path, we can use the&nbsp;<code>Path.read_text()<\/code>&nbsp;method.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import Path\n\npath = Path('sample_file.txt').read_text(encoding='utf-8')\nprint(path)\n\n----------\nHi, I am sample file for testing.<\/pre><\/div>\n\n\n\n<p><strong>Writing data to the file<\/strong><\/p>\n\n\n\n<p>The&nbsp;<code>Path<\/code>&nbsp;class provides a&nbsp;<code>.write_text()<\/code>&nbsp;method for writing text data to a file.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import Path\n\n# Instantiating the path of the file\npath = Path('sample_file.txt')\n# Writing data to the file\npath.write_text('Hello from GeekPython.')\n\n# Reading the data\nprint(path.read_text())\n\n----------\nHello from GeekPython.<\/pre><\/div>\n\n\n\n<p>Similarly, we can use the&nbsp;<code>Path.write_bytes()<\/code>&nbsp;method to write binary data to a file. It opens the file in binary mode.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Instantiating the path of the file\npath = Path('sample_file.txt')\n# Writing binary data to the file\npath.write_bytes(b'Hello from GeekPython.')\n# Reading the binary data\nprint(path.read_bytes())\n\n----------\nb'Hello from GeekPython.'<\/pre><\/div>\n\n\n\n<p>We wrote the binary data to the&nbsp;<code>sample_file.txt<\/code>&nbsp;but if we look at the code, we read the file content using the&nbsp;<code>.read_bytes()<\/code>&nbsp;method.<\/p>\n\n\n\n<p><code>Path.read_bytes()<\/code>&nbsp;opens the file in binary mode and returns the contents of the file as a byte string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/python-pathlib-module#heading-conclusion\"><\/a>Conclusion<\/h2>\n\n\n\n<p>The&nbsp;<code>pathlib<\/code>&nbsp;module provides high-level classes for manipulating file paths. These classes can be used to perform various operations on file paths as well as interact with files to perform I\/O operations.<\/p>\n\n\n\n<p>Let&#8217;s recall what we&#8217;ve learned:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Pure path and Concrete path classes<\/li>\n\n\n\n<li>Path operations using the&nbsp;<code>PurePath<\/code>&nbsp;class<\/li>\n\n\n\n<li><code>Path<\/code>&nbsp;class for instantiating concrete paths<\/li>\n\n\n\n<li>Methods of the&nbsp;<code>Path<\/code>&nbsp;class<\/li>\n\n\n\n<li>Reading and writing files<\/li>\n\n\n\n<li>Modifying the filesystem<\/li>\n<\/ul>\n\n\n\n<p>Reference &#8211;&nbsp;<a target=\"_blank\" href=\"http:\/\/docs.python.org\/3\/library\/pathlib.html\" rel=\"noreferrer noopener\">docs.python.org\/3\/library\/pathlib.html<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might be interested in if you liked this one<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/shutil-module-in-python\" rel=\"noreferrer noopener\">Perform high-level file operation using shutil module in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/zipfile-read-and-write-zip-files-without-extracting-it-in-python\" rel=\"noreferrer noopener\">Read and write zip files without extracting them in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/handling-files-in-python\" rel=\"noreferrer noopener\">File handling in Python &#8211; Open, read, and write<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/tempfile-in-python\" rel=\"noreferrer noopener\">Generate temporary files and directories using tempfile module in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/seek-and-tell-in-python\" rel=\"noreferrer noopener\">What is the difference between seek() and tell() in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/context-managers-and-python-with-statement\" rel=\"noreferrer noopener\">A comprehensive guide to context manager and with statement in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/how-to-open-and-read-multiple-files-using-with-in-python\" rel=\"noreferrer noopener\">Open and read multiple files simultaneously using with statement in Python<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The&nbsp;pathlib&nbsp;module is a part of Python&#8217;s standard library and allows us to interact with filesystem paths and work with files using various methods and properties on the&nbsp;Path&nbsp;object. Getting Started With pathlib The most frequently used class of the&nbsp;pathlib&nbsp;module is&nbsp;Path. It is better to kick off with the&nbsp;Path&nbsp;class if we are using this module for the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1125,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2,42],"tags":[43,12,31],"class_list":["post-1123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-modules","tag-modules","tag-python","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=1123"}],"version-history":[{"count":5,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1123\/revisions"}],"predecessor-version":[{"id":1288,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1123\/revisions\/1288"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/1125"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=1123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=1123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=1123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}