title: Adding a suffix to a full path's name, before the extension
body: <p>I am moving some of my code from <a href="https://docs.python.org/3/library/os.path.html" rel="nofollow noreferrer"><code>os.path</code></a> to <a href="https://docs.python.org/3/library/pathlib.html" rel="nofollow noreferrer"><code>pathlib.Path</code></a> and found that in general it is much better.</p>
  
  <p>On a specific task I found that actually <a href="https://docs.python.org/3/library/os.path.html" rel="nofollow noreferrer"><code>os.path</code></a> might be more comfortable to use. I want to create a new path from a given one, by adding a suffix to its name and keeping the same root and extension. For example, from:</p>
  
  <pre><code>/a/b/c/file.txt
  </code></pre>
  
  <p>I want to get:</p>
  
  <pre><code>/a/b/c/file_test.txt
  </code></pre>
  
  <p>Using <a href="https://docs.python.org/3/library/os.path.html" rel="nofollow noreferrer"><code>os.path</code></a>, this can be done easily with <a href="https://docs.python.org/3/library/os.path.html#os.path.splitext" rel="nofollow noreferrer"><code>splitext</code></a>:</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; import os
  &gt;&gt;&gt; path = "/a/b/c/file.txt"
  &gt;&gt;&gt; base, ext = os.path.splitext(path)
  &gt;&gt;&gt; base + "_test" + ext
  '/a/b/c/file_test.txt'
  </code></pre>
  
  <p>But, going over the <a href="https://docs.python.org/3/library/pathlib.html" rel="nofollow noreferrer"><code>pathlib</code>'s docs</a>, I found <a href="https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.with_name" rel="nofollow noreferrer"><code>with_name</code></a> and <a href="https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.with_suffix" rel="nofollow noreferrer"><code>with_suffix</code></a> and got something like:</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; from pathlib import Path
  &gt;&gt;&gt; path = Path("/a/b/c/file.txt")
  &gt;&gt;&gt; path.with_suffix('').with_name(path.stem + "_test").with_suffix(path.suffix)
  PosixPath('/a/b/c/file_test.txt')
  </code></pre>
  
  <p>Which I believe is far worse than the <a href="https://docs.python.org/3/library/os.path.html" rel="nofollow noreferrer"><code>os.path</code></a> version.</p>
  
  <p>Is there a better, cleaner way of achieving this with <a href="https://docs.python.org/3/library/pathlib.html" rel="nofollow noreferrer"><strong><code>pathlib</code></strong></a>? </p>