title: Python: change part (single directory name) of path
body: <p>What would be the best way to change a single directory name (only the first occurence) within a path?</p>
  
  <p>Example:</p>
  
  <pre><code>source_path = "/path/to/a/directory/or/file.txt"
  target_path = "/path/to/different/directory/or/file.txt"
  </code></pre>
  
  <p>I this case, the instruction would be: "replace the first directory of the name 'a' with a directory of the name 'different'"</p>
  
  <p>I can think of methods where I would split up the path in its single parts first, then find the first "a", replace it and join it again. But I wonder if there is a more elegant way to deal with this. Maybe a built-in python function.</p>