soledad penadés
repeat 4[fd 100 rt 90]

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))

Download from svn (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 :-)

// 9 responses to Split files into folders by letter

pplux
pplux
20080513

I'm wondering (if you are using bash) why not something like:

$ ls | while read file; do mkdir -p ${file:0:1}; mv ${file} ./${file:0:1}; done

:)

Bash-man strikes again!

sole
sole
20080513

Ha! I knew you were going to say something. Maybe due to the fact that I was using python and you've got that anti-python for all feeling :-P

I hadn't thought of using bash really but that one-liner is super! Does it also convert the letters to lowercase? and check if the folder already exists?

iyo
iyo
20080513

As I read one-line script from pplux I can answer your questions:
- it doesn`t convert letters to lowercase
- it doesn`t check if the folder already exists, but parameter -p write no errors in case of duplicate folder and silently do nothing

sole
sole
20080513

Yup, I knew about -p but I wondered if the oneliner would secretly convert to lowercase…

It also assumes that there aren't spaces in the files, and also tries to move folders, which I do not want to move :D

No minipoint yet, pplux, hehehe

pplux
pplux
20080513

Oh my goodness!! first I'm not an anti-python, I'm just a "not use python for everything", I like python (not as much as other languages, but, I like it.)

second…

a) The spaces in folders could be easily solved by adding quotation marks :

$ ls | while read file; do mkdir -p ${file:0:1}; mv "${file}" "./${file:0:1}"; done

and b)… ok, the upper to lower thing and, why not, avoid moving a directory to itself:

$ ls | while read file; do d=$(echo ${file:0:1}| tr '[[:upper:]]' '[[:lower:]]'); [ -d $d ] || (mkdir -p $d; mv "${file}" "./$d"); done

… It was so beautiful at the very beginning, now you both have spoiled the party XD

Cheers!

PS: of course, I haven't tested them… be careful (with the hidden sudo rm -rf /)

pplux
pplux
20080514

Mmmm… Today, while having a shower, I realized the last one is wrong !

Hint: The problem is around [ -d $d ]

sole
sole
20080514

Can't have a look right now but will do soon :D

Zarate
Zarate
20080515

Sorry for the non-Spanish speakers, but Sole, have you seen this?:

http://mundogeek.net/archivos/2008/05/06/el-tutorial-de-python-en-pdf/

I've been meaning to give a go to Python but never actually done it, that tuto looks like the perfect excuse : )

sole
sole
20080516

No… I haven't required anything else which wasn't in Python's documentation or that I couldn't find in a quick google search. That said, Python docs are magnificently well written. There's even a tutorial step by step which looks a lot like that tutorial you suggest ;)

Feel free to leave a reply

Comments are moderated: Rude and offtopic ones are out!