Path manipulation in python
<p>I have looked on Stack Overflow everywhere but I cant find a solution to this problem.</p>

<p>Given that I have a folder/file as string: <code>"/path1/path2/path3/file"</code> how can I get the parent folder and its parent folder. In other words if I want to traverse up one level <code>"/path1/path2/path3"</code> or two levels <code>"/path1/path2"</code> how can I get those string values from the original string path in python?</p>

<p>Please note that I <strong>don't simply want the pieces</strong> of the path (in other words not a list of <code>['path1', 'path2', 'path3']</code>) but instead <code>"/path1/path2/path3"</code>.</p>

<p>Thank you </p>

<pre><code>'/'.join (originalPath.split ('/') [:-2])
</code></pre>

<p>It will take you 2 levels up. </p>

python