Making hierarchy of folders using Python


Suppose you want to make many folders aka directories for some purposes. Maybe you need to make many folders in a relative’s computers? Maybe you want to make many folders with same pattern in the names? Maybe someone has a habit of deleting files or formatting his hard drive and you have the responsibility of making a basic folder hierarchy>

I posted how to delete files so I thought to add one to make files also.

The hierarchy of directories that I am making is like this. Every indent means a subdirectory of the previous unindented one.

./project_euler
    ./001_050
    ./051_100
    ...
    ./401_450
./codechef
    ./easy
    ./medium
    ./hard
./spoj
./functions
./utilities

I made this script to learn Python mainly but it can be used to do same task of creating directories again and again easily. There are alternatives in Unix/Linux but in Windows it might be the easiest way.

#! python3
import os

TOP_LEVEL = ('spoj', 'functions', 'utilities', '_testing')
EULER_HIGHEST = 450
CODECHEF_FOL = ('easy', 'medium', 'hard')

def safe_make_folder(i):
    '''Makes a folder and its parent if not present'''
    try:
        os.makedirs(i)
    except:
        pass

def make_top_level():
    '''Makes folders with no subdirectories'''
    for i in TOP_LEVEL:
        safe_make_folder(i)

def make_euler_folders():
    '''Makes euler and its subdirectories'''
    for j in (os.path.join('project_euler', '{:03}_{:03}'.format(i, i + 49))
              for i in range(1,EULER_HIGHEST, 50)):
        safe_make_folder(j)

def make_codechef_folders():
    '''Makes codechef and its subdirectories'''
    for i in CODECHEF_FOL:
        safe_make_folder(os.path.join('codechef', i))

def main():
    make_top_level()
    make_euler_folders()
    make_codechef_folders()

if __name__ == "__main__":
    main()

This script’s comments are self-explanatory. Any questions? Just ask in the comments?

Opening websites using Python


Perhaps you need to open a set of websites for doing some work? Maybe opening facebook, twitter and other social networking websites? But you don’t want to click on many bookmarks or type the website’s name in your browser. Then this solution is for you. You can write different scripts for opening different set of websites. You can decide their sequence and even adjust the time between them.

Just change the names of websites in URLS, place them inside quotes ‘xyz_website’ and separate all the websites’ name by commas. Put any number in DELAY variable for the delay in seconds you want between opening of the webpages and you are good to go.

#! python3
import webbrowser
import time

URLS = [
    'http://codereview.stackexchange.com/',
    'http://programmers.stackexchange.com/',
    'http://stackoverflow.com/'
    ]
DELAY = 2

def main():
    for URL in URLS:
        webbrowser.open(URL, 2)
        time.sleep(DELAY)main()

A lot of links for learning, practicing and other things related to programming


I thought today that I had a tough time finding appropriate links to learn programming so maybe everybody else who is learning may be having a similar problem. I decided to make a page containing a lot of links for everything related to programming – learning new languages, practicing etc.

It’s a work-in-progress but I already put many links to huge lists of links. Check it out.

Python, java and a lot of resources


Ok so I have continued with codecademy‘s  Python track. Two-third done already.  For anyone reading I would give this suggestion. Codecademy is for beginner’s with no programming experience. It can give you a feel of the language but cannot teach you programming. It’s Python track covers the language basics but could use improvements.

Its 7th Track part-2 Battleship can use a lot of improvements. The instructions miss out on some points like corner cases in case of this project. Also the online judging system lets wrong codes pass through them.

I had the feeling that I would be needing more text on Python to learn it so I started with Python tutorials. They are good so far.

I found listings of a lot of programming resources  on stackoverflow.
Each programming language has an info page which contains listing of a lot of relevant material. I wish I knew that when I started out.

I was focusing on Python for past few days so I gave Java some time. Learned how to make executable files for Java in Netbeans and started experimenting with GUI Builder using JFrames container.