[Tut] Extract File Name From the Path, No Matter What the os/path Format - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] Extract File Name From the Path, No Matter What the os/path Format (/thread-99345.html) |
[Tut] Extract File Name From the Path, No Matter What the os/path Format - xSicKxBot - 05-06-2022 Extract File Name From the Path, No Matter What the os/path Format <div><p class="has-global-color-8-background-color has-background"><strong>Summary: </strong><code>os.path.basename(path)</code> enables us to get the file name from the path, no matter what the os/path format. Another workaround is to use the <code>ntpath</code> module, which is equivalent to <code>os.path</code>.</p> <hr class="wp-block-separator" /> <p><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/2728.png" alt="✨" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Problem: </strong>How to extract the filename from a path, no matter what the operating system or path format is? </p> <p>For example, let’s suppose that you want all the following paths to return <code>demo.py</code>:</p> <pre class="wp-block-code"><code>➤ C:\Users\SHUBHAM SAYON\Desktop\codes\demo.py ➤ /home/username/Desktop/codes/demo.py ➤ /home/username/Desktop/../demo.py</code></pre> <p><strong>Expected Output in each case:</strong></p> <pre class="wp-block-code"><code>demo.py</code></pre> <p class="has-base-2-background-color has-background has-medium-font-size"><strong>Recommended: <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-get-the-filename-without-the-extension-from-a-path-in-python/" target="_blank">How To Get The Filename Without The Extension From A Path In Python?</a></strong></p> <p>Let us dive into the solutions without further delay.</p> <h2>❖ <strong>Method 1: Using os.path.basename</strong></h2> <p><code>os.path.basename</code> is a built-in method of the os module in Python that is used to derive the basename of a file from its path. It accepts the path as an input and then returns the basename of the file. Thus, to get the filename from its path, this is exactly the function that you would want to use.</p> <p><strong>Example 1: In Windows</strong></p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import os file_path = r'C:\Users\SHUBHAM SAYON\Desktop\codes\demo.py' print(os.path.basename(file_path)) # OUTPUT: demo.py</pre> <p><strong>Example 2: In Linux</strong></p> <figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="511" height="196" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-26.png" alt="" class="wp-image-337962" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-26.png 511w, https://blog.finxter.com/wp-content/uploads/2022/05/image-26-300x115.png 300w" sizes="(max-width: 511px) 100vw, 511px" /></figure> <p><strong>Caution: </strong>If you use the <code>os.path.basename()</code> function on a POSIX system in order to get the basename from a Windows-styled path, for example: “<em><code>C:\\my\\file.txt</code></em>“, the entire path will be returned.</p> <p><strong>Tidbit: </strong><code>os.path.basename()</code> method actually uses the <code>os.path.split()</code> method internally and splits the specified path into a<em> head </em>and<em> tail </em>pair and finally returns the tail part. </p> <h2>❖ <strong>Method 2: Using the ntpath Module</strong></h2> <p>The <code>ntpath</code> module can be used to handle Windows paths efficiently on other platforms. <code>os.path.basename</code> function does not work in all the cases, like when we are running the script on a Linux host, and you attempt to process a Windows-style path, the process will fail. </p> <p>This is where the <code>ntpath</code> module proves to be useful. Generally, the Windows path uses either the backslash or the forward-slash as a path separator. Therefore, the <code>ntpath</code> module, equivalent to the <code>os.path</code> while running on Windows, will work for all the paths on all platforms.</p> <p>In case the file ends with a slash, then the basename will be empty, so you can make your own function and deal with it:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import ntpath def path_foo(path): head, tail = ntpath.split(path) return tail or ntpath.basename(head) paths = [r'C:\Users\SHUBHAM SAYON\Desktop\codes\demo.py', r'/home/username/Desktop/codes/demo.py', r'/home/username/Desktop/../demo.py'] print([path_foo(path) for path in paths]) # ['demo.py', 'demo.py', 'demo.py']</pre> <h2>❖ <strong>Method 3: Using pathlib.Path()</strong></h2> <p>If you are using Python 3.4 or above, then the <code>pathlib.Path()</code> function of the pathlib module is another option that can be used to extract the file name from the path, no matter what the path format. The method takes the whole path as an input and extracts the file name from the path and returns the file name.</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from pathlib import Path file_path = r'C:\Users\SHUBHAM SAYON\Desktop\codes\demo.py' file_name = Path(file_path).name print(file_name) # demo.py</pre> <p><strong>Note:</strong> The <code>.name</code> property followed by the pathname is used to return the full name of the final child element in the path, regardless of whatever the path format is and regardless of whether it is a file or a folder.</p> <p><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Bonus Tip: </strong>You can also use <code>Path("File Path").stem</code> to get the file name without the file extension.</p> <p><strong>Example:</strong></p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from pathlib import Path file_path = r'C:\Users\SHUBHAM SAYON\Desktop\codes\demo.py' file_name = Path(file_path).stem print(file_name) # demo</pre> <h2>❖ <strong>Method 4: Using <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-split/" target="_blank">split()</a></strong></h2> <p>If you do not intend to use any built-in module to extract the filename irrespective of the OS/platform in use, then you can simply use the <code>split()</code> method. </p> <p><strong>Example:</strong></p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import os file_path = r'C:\Users\SHUBHAM SAYON\Desktop\codes\demo.py' head, tail = os.path.split(file_path) print(tail) # demo.py</pre> <p><strong>Explanation: </strong>In the above example <code><em><strong>os.path.split()</strong></em></code> method is used to split the entire path string into <em>head</em> and <em>tail</em> pairs. Here, <em>tail</em> represents/stores the ending path name component, which is the base filename, and <em>head</em> represents everything that leads up to that. Therefore, the tail variable stores the name of the file that we need. </p> <p class="has-background" style="background-color:#f1e3ed">➤ <strong>A Quick Recap to split(): </strong><br /><code>split()</code> is a built-in method in Python that splits a string into a list based on the separator provided as an argument to it. If no argument is provided, then by default, the separator is any whitespace. </p> <p>Learn more about the <code>split()</code> method <strong><a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-split/" target="_blank">here</a></strong>.</p> <p>Alternatively, for more accurate results you can also use a combination of the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-strip/" target="_blank">strip()</a> and <code>split()</code> methods as shown below.</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">file_path = r'C:\Users\SHUBHAM SAYON\Desktop\codes\demo.py' f_name = file_path.strip('/').strip('\\').split('/')[-1].split('\\')[-1] print(f_name) # demo.py</pre> <p><strong>Explanation: </strong>The <code>strip</code> method takes care of the forward and backward slashes, which makes the path string fullproof against any OS or path format, and then the <code>split</code> method ensures that the entire path string is split into numerous strings within a list. Lastly, we will just return the last element from this list to get the filename. </p> <h2>❖ <strong>Method 5: Using <a rel="noreferrer noopener" href="https://blog.finxter.com/python-regex/" target="_blank">Regex</a></strong></h2> <p>If you have a good grip on regular expressions then here’s a regex specific solution for you that will most probably work on any OS. </p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import re file_path = r'C:\Users\SHUBHAM SAYON\Desktop\codes\\' def base_name(path): basename = re.search(r'[^\\/]+(?=[\\/]?$)', path) if basename: return basename.group(0) paths = [r'C:\Users\SHUBHAM SAYON\Desktop\codes\demo.py', r'/home/username/Desktop/codes/demo.py', r'/home/username/Desktop/../demo.py'] print([base_name(path) for path in paths]) # ['demo.py', 'demo.py', 'demo.py']</pre> <hr class="wp-block-separator" /> <p><strong><em>Do you want to master the regex superpower?</em></strong> Check out my new book <em><strong><a href="https://blog.finxter.com/ebook-the-smartest-way-to-learn-python-regex/" target="_blank" rel="noreferrer noopener" title="[eBook] The Smartest Way to Learn Python Regex">The Smartest Way to Learn Regular Expressions in Python</a></strong></em> with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video. </p> <h2><strong>Conclusion</strong></h2> <p>To sum thungs up, you can use one of the following methods to extract the filename from a given path irrespective of the OS/path format:</p> <ul> <li><code data-enlighter-language="generic" class="EnlighterJSRAW">os.path.basename('path')</code></li> <li><code data-enlighter-language="generic" class="EnlighterJSRAW">ntpath.basename()</code></li> <li><code data-enlighter-language="generic" class="EnlighterJSRAW">pathlib.Path('path').name</code></li> <li><code data-enlighter-language="generic" class="EnlighterJSRAW">os.path.split('path')</code></li> <li><code data-enlighter-language="generic" class="EnlighterJSRAW">using regex</code></li> </ul> <p>Please <strong><a href="https://www.youtube.com/channel/UCRlWL2q80BnI4sA5ISrz9uw" target="_blank" rel="noreferrer noopener">stay tuned</a></strong> and <strong><a href="https://blog.finxter.com/subscribe/" target="_blank" rel="noreferrer noopener">subscribe</a></strong> for more interesting articles!</p> <hr class="wp-block-separator" /> <p>To become a PyCharm master, check out our <a href="https://academy.finxter.com/university/pycharm/" title="https://academy.finxter.com/university/pycharm/" target="_blank" rel="noreferrer noopener">full course</a> on the Finxter Computer Science Academy available for free for all <a href="https://blog.finxter.com/finxter-premium-membership/" target="_blank" rel="noreferrer noopener" title="Finxter Premium Membership">Finxter Premium Members</a>:</p> <div class="wp-block-image"> <figure class="aligncenter size-full"><a href="https://academy.finxter.com/university/pycharm/" target="_blank" rel="noopener"><img loading="lazy" width="363" height="650" src="https://blog.finxter.com/wp-content/uploads/2021/09/image-10.png" alt="" class="wp-image-34968" srcset="https://blog.finxter.com/wp-content/uploads/2021/09/image-10.png 363w, https://blog.finxter.com/wp-content/uploads/2021/09/image-10-168x300.png 168w" sizes="(max-width: 363px) 100vw, 363px" /></a></figure> </div> </div> https://www.sickgaming.net/blog/2022/05/02/extract-file-name-from-the-path-no-matter-what-the-os-path-format/ |