Split files into folders by letter

I had a lot of files in one folder. It is not very practical to browse the folder that way, so I decided to create a little script which would split the files into different folders, using the first letter of the file for naming the folder, as in a, b, c, d... but using Python this time!


import os
import shutil
import sys

if len(sys.argv) > 1:
    folder = sys.argv[1]
else:
    folder = '.'

for item in os.listdir(folder):

    full_path = os.path.join(folder, item)

    if os.path.isdir(full_path):
        continue

    dst_folder = os.path.join(folder, item[0].lower())

    if not os.path.exists(dst_folder):
        os.mkdir(dst_folder)

    shutil.move(full_path, os.path.join(dst_folder, item))

github (this should always be the most up to date version)

To use simply go to your favourite terminal window and type


python split_files_into_folders_by_letter.py /path/to/messy/folder

It will pick the first letter of each file, create a folder with the lowercased letter (if it doesn't exist yet) and move the file to the folder. It won't move folders! (because of the os.path.isdir check).

If there's any pythonista in the audience with ideas for improving this, feel free to leave your suggestions in the comments, thanks! :-)

Note

There wasn't any special reason for not doing it with Ruby; I simply wanted to know what the differences would be. And while the semantics aren't very different (e.g. where it says os.path.join we would use File.join in Ruby), there is a huge difference between them and it's called documentation. Python documentation is hosted in Python's website, it is reviewed (and mostly written too) by Guido van Rossum (Python's author) and it's up to date and very easy to browse and read. Whereas Ruby docs were hosted elsewhere until very recently, are a pain to browse and pretty much a dump of automatically generated docs from source, which I find very uncomfortable to use. It still feels a little bit weird to use python's indenting style but I'm really liking it. A pity there isn't a Hpricot in python :-)