Path manipulation functions in Python

Spread the word!

Motivation

There are many built-in functions in the Python os module about paths manipulation. In this post I introduce some custom path manipulations.

How to take the first n parts of the path? How to take the last n parts of the path? How to remove last n parts (or only folders) from path? The examples are self-explaining.

Code

Examples

Output

C:
Folder4\File.txt
C:\Folder1\Folder2\Folder3\Folder4
C:\Folder1\Folder2\Folder3\File.txt

Be the first to comment

Leave a Reply

Your email address will not be published.


*