In order to take a break from making Flask apps, my Bittorrent client, messing with various databases, and reading HTTP protocol documentation, I wanted to write about Python, something I know.
While working at Dreamworks Animation, my favorite module was the "path" module. This is a 3rd party module dealing with file paths. It makes paths as first class objects, allowing you to do some common operations on them. We had our own version at Dreamworks, which hooks into our proprietary software. But path.py is still useful by itself. This is way better than using os.path. Hopefully, after this blog post, you will never use os.path... ever.
You might think module sounds boring. I mean, file directories... really? Well, when you are working with hundreds of thousands of images (30 frames in a second, 90 minutes in an animated film, so we are talking 30 x 60 x 90=162,000 images for an entire movie, not including composite layers that make up one image), this module is a godsend. We were constantly renaming, deleting, copying and moving files all over the place. Most of the marketing tasks were frankensteining some frames together in order to meet deadlines.
But you can use this module in everyday tasks! Like cleaning up unnecessary data or making a script to rename some music files in Python! Here are some examples of using this module! If you are following along, please start in an empty directory.
First lets install this module.
Okay, time to start playing with it!
from path import path
root = path('.')
This will create a path object that is pointed to your current directory! One of my favorite features is how easy and readable it is to append directories to the path. For example, you can do this!
In : t = root / "test" / "a" / "long" / "directory" / "tree"
In : print t
How awesome is that?? You can even ask if the directory exists or not.
In : t.exists()
Lets go ahead and make this directory!
In : t.makedirs()
In : t.exists()
How easy was that?! You can also give it permissions with the
mode flag! By default, permissions is set to 511.
Let's make a lot of directories!
for i in range(10):
s = root / str(i)
for i in range(random.randint(0,10)):
s = s / str(i)
This function creates 10 directories with random directory depth, within a range from 0 to 10. Lets see if this worked
In : root.dirs()
Lets create a file at the end of directory depth!
for dir in root.walkdirs():
if not dir.dirs():
file = dir / "myfile"
walkdirs returns a generator that iterates through your directory structure until you've looked at every folder. It is basically doing in-order traversal.
We can use
walkfiles to see if we created the necessary files!
In : for files in root.walkfiles():
.....: print files
Okay, lets try to rename my numbered directories into letters! Just because we can.
for dir in root.dirs():
parent, d = dir.splitpath()
dir.rename(parent / chr(ord('a') + int(dir.name)))
Woah! A lot of stuff going on here! I wrote a lot of these little recursive functions a lot, but you can do the same thing as
walkdirs. Basically, I loop over all the directories in the current directory, and check to see if they have any directories. If so, I recurse. When the function returns, I rename the directory to the ASCII equivalent by using
rename path is a little tricky, as it needs the full path from where your Python module is running from. That is why I call
splitpath in order to get the parent directory. Now, we have folders like this!
a/ b/ c/ d/ e/ f/ g/ h/ i/ j/
Now we have a directory structure like this! (Note: I shorten the results for brevity.)
So now that we are done testing, time to delete these useless directories!
for dir in root.dirs():
This will delete the entire directory tree of the current folder. How cool is that? Don't run this if you have any import files or directories in your root folder!!!
Now that you know the basics, check out this blog about how to write a script a clean-up script for your website!
Python 101: Writing a cleanup script
There are comments.