<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Iyanuoluwa Ajao</title>
    <description>The latest articles on DEV Community by Iyanuoluwa Ajao (@iyanuashiri).</description>
    <link>https://dev.to/iyanuashiri</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F39343%2F669b3edd-3307-401a-8517-82b320af32ac.jpeg</url>
      <title>DEV Community: Iyanuoluwa Ajao</title>
      <link>https://dev.to/iyanuashiri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/iyanuashiri"/>
    <language>en</language>
    <item>
      <title>Getting Started with Pathlib</title>
      <dc:creator>Iyanuoluwa Ajao</dc:creator>
      <pubDate>Fri, 30 Nov 2018 00:12:32 +0000</pubDate>
      <link>https://dev.to/iyanuashiri/getting-started-with-pathlib-33n4</link>
      <guid>https://dev.to/iyanuashiri/getting-started-with-pathlib-33n4</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This tutorial will guide you on how to use the Pathlib module for working with filesystem paths, the benefits, and understand the problem it solves since the Python standard library already contains &lt;code&gt;os.path.&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How is it Better than os.path
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;os.path&lt;/code&gt; was the only way to work with filesystem paths, but it had some limitations. To perform certain tasks, you&lt;br&gt;
have to import from other standard library modules. For instance, suppose you want to list the files ending with &lt;code&gt;.txt&lt;/code&gt;&lt;br&gt;
in a directory, &lt;code&gt;os.path&lt;/code&gt; is not enough.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;os.path&lt;/code&gt;, you will do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;glob&lt;/span&gt;

&lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;glob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'posts'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'*.md'&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;pathlib&lt;/code&gt;, you will do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;pathlib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;

&lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'posts'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;glob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'*.md'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the first example that makes use of &lt;code&gt;os.path&lt;/code&gt;, there was the need to import &lt;code&gt;glob&lt;/code&gt;. But with &lt;code&gt;pathlib&lt;/code&gt;, most of the functionality found in other modules are now in one place. &lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;pathlib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;According to the standard library, Path instantiates a concrete path for the platform your code is running on.&lt;br&gt;
This basically means that Path class figures out the necessary path separator that is required for the platform&lt;br&gt;
your code is running on. &lt;/p&gt;

&lt;p&gt;NB: Windows uses a backslash as a path separator while unix based systems use the forward slash.&lt;/p&gt;

&lt;p&gt;There are other classes that can be used, such as the &lt;code&gt;WindowsPath&lt;/code&gt; and &lt;code&gt;PosixPath&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Using Path classmethods
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;PosixPath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/home/ashiri'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cwd&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;PosixPath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/home/ashiri/iyanuashiri/content/posts'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The Path class provides us with two classmethods. The &lt;code&gt;home&lt;/code&gt; classmethod returns a home directory path object while the&lt;br&gt;
&lt;code&gt;cwd&lt;/code&gt; classmethod returns the current working directory path object&lt;/p&gt;
&lt;h2&gt;
  
  
  Creating Path object
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="s"&gt;'iyanu'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;PosixPath('/home/ashiri/iyanu')&lt;/p&gt;

&lt;p&gt;This code snippet creates a path object that can be used in your Python code, but the directory already exist&lt;br&gt;
for the code to do something. In the next two examples, we will show how to create a new directory and a new file from&lt;br&gt;
your Python code.&lt;/p&gt;
&lt;h2&gt;
  
  
  Create a New Directory
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="s"&gt;'folder_name'&lt;/span&gt;
&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mkdir&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In the code snippet above, we create a path object called &lt;code&gt;p&lt;/code&gt; and use the instance method &lt;code&gt;mkdir&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Create a New File
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="s"&gt;'folder_name'&lt;/span&gt;
&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mkdir&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="s"&gt;'file_name.txt'&lt;/span&gt;

&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;touch&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In this code snippet, we start by creating a path object called &lt;code&gt;p&lt;/code&gt;, then use the &lt;code&gt;mkdir()&lt;/code&gt; instance method.&lt;br&gt;
The next thing is create another path object called &lt;code&gt;r&lt;/code&gt; and use the instance method &lt;code&gt;touch()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;mkdir(exist_ok=True)&lt;/code&gt; method can take in an argument. This prevents a &lt;code&gt;FileExistsError&lt;/code&gt; from being raised.&lt;/p&gt;
&lt;h2&gt;
  
  
  Opening a File in a Path
&lt;/h2&gt;

&lt;p&gt;There is an &lt;code&gt;open&lt;/code&gt; instance method that works like the builtin &lt;code&gt;open&lt;/code&gt; function. So we can do something&lt;br&gt;
like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;open&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readline&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  In conclusion
&lt;/h2&gt;

&lt;p&gt;There is more than can be learnt on the pathlib documentation page, which can be found &lt;a href="https://docs.python.org/3/library/pathlib.html"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you enjoyed this article, don't forget to share and comment below. Follow me on Twitter: &lt;a href="https://www.twitter.com/iyanuashiri"&gt;@IyanuAshiri&lt;/a&gt;, I tweet about Python. You can also checkout my blog &lt;a href="https://iyanuashiri.me"&gt;iyanuashiri.me&lt;/a&gt; for more&lt;br&gt;
Python content. &lt;/p&gt;

</description>
      <category>python</category>
    </item>
  </channel>
</rss>
