Wednesday, February 17, 2010

File browser using Python

I always wanted to have a file browser ( like nautilus ) on my shell. Something like the image on the left.
I prefer to have my hands on the keyboard than shifting between keyboard and the tailed buddy.
I saw my friend Lee use something called Midnight Commander. I googled and found other alternatives here. But only MC was appealing to me. I started using it and it is a decent one. But I was not satisfied with its display. It shows all the files with in one big column and displays all the details as if some had typed "ls -l". And recently after switching to vi mode, I am finding it hard to access the arrow keys and would prefer something similar to vi. One way of achieving it is actually remapping keys in MC. But still the concept of moving left and right does not have a meaning in MC context. Moreover, we are used to things that behave like nautilus. I wanted a file browser that displays folders and files like nautilus and makes it possible to navigate using hjkl keys. I also wanted a mechanism where I could tag files just like in delicious. I finally decided I want a new file browser and I am going to write it :) .

My obvious and only choice is python. Although there is bash scripting with zenity to make your life easy, I went for python for the obvious reason. I also want the fun of coding in python. I also wanted to try out coding in python on my newly configured Vim. My idea of programming on terminals was limited to printing something on terminal. I tried out something like that long back using strings. But it was a very naive way of doing things and I didn't like it. One day I tried out something called bpython. It is another command line interpreter for python. I was fascinated by the way the program works. It is simply amazing and behaves like a proper GUI IDE on shell. Luckily my friend Madhu told me that this is written using ncurses. The word curses caught me. I thought may be it can help me out in writing my file browser. But I didnt give it much thought. I actually tried out some stuff only to see that it requires special way of approach.

So now here I am, decided to write a file browser and know a word ncurses. Where do I start... obvious google. I found a few links on ncurses and learnt that it also has a wrapper in python called "curses". But then I realised this is exactly what I did when I tried ncurses for the first time and got no where. I looked back and found out that the reason I stopped is because the moment you try to manipulate your terminal, the default behaviour of your python interpreter is lost and it gets really annoying to proceed. All you can do is write code in a file and run it every time. I wondered how would bpython behave!!. I tried basic importing and initialising screen and to my delight, it did not have any effect on bpython console. bpython being written using curses, overwrites all my actions. My interpreter is working fine. So this also has a disadvantage. Although you can see all the attributes available for any object since bpython displays attributes after you type a dot, you cant really see any change that you make. Anyways I still will have to stick to my old way of doing, writing in a file and executing.

I found a few links on how to program in curses using python.
These links got me started and python curses man page helped me through the process.

And that is how I end up with something that suits perfectly to my requirements.
Currently this is the key mapping on my PyBrowser that opens with files in current directory.
  • h - move left
  • j - move down
  • k - move up
  • l - move right
  • b - back one directory
  • o - open the directory or file( programs are mapped to filenames in a different conf file )
  • H - Toggle hidden files
  • q - close or quit
All this in 200 lines of code and 2 days of work with loooots of breaks. Now you know why I code in Python ;) .Shall add more functionality later on.


  1. Talk is Cheap. Show me th Code. :D
    /me will also try it out. I want a file browser for my minimalistic system.

  2. @punch

    Nice... I didn't know about this vifm. It is pretty good. Still I want the nautilus kind of interface on shell.

  3. great, code's simple and pretty straight forward. If you want to extend it, here's an idea, a table which maps the type of file to default app which opens the map, and then we can make the pybrowse to open it using 'command-name [-options] filename &'