Path building and manipulation

You will often have to construct file system paths using Python. For example, given the variable mydir which points to some path, you may want to get the path two folders up:

otherdir = mydir + '\\..\\..'

Do not build or manipulate paths this way. There are a number of things wrong with this code. The obvious problem is that it is platform-dependent. Windows and Linux-based systems use different path separators.

Even more importantly, this code (and code that uses the value of otherdir) can break depending on the value of mydir and which operating system is being used. In Windows, drive roots like C:\ are usually returned with a trailing slash, while other folders, like C:\Windows, are not. There are all sorts of other ...

Get Practical Maya Programming with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.