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 = [

def main():
    for URL in URLS:, 2)

Started writing a simple web crawler in Python for downloading a website

I have been having trouble looking through the C standard library so I thought to download the C library reference from I just wrote a simple script for downloading the front page and it is currently missing the style on the website but it got the basic contents. I’ll need to some work before it gives the best result.

I need to add code for recursively download everything but it’s a start.

Here’s my Python script.

#! python3
import urllib.request

filehandle = urllib.request.urlopen(

with open('test.html', 'w+b') as f:
    for line in filehandle:


Excuse for cleaning up my code base

I finally got an excuse for cleaning up my code base – Upgrading from Python 2 to python 3. After exactly 2 months with Python 2 I decided to upgrade to Python 3 after a discussion on comp.lang.python. I am glad I am doing that.

I used this excuse to finally merge me C and Python repo that I wanted to do but just wasn’t doing because I didn’t want to do a huge merge without a reason. Now most of my Python code will go through a round of cleaning. All the code that I wrote a complete newbie would be cleaned. I am really glad. That will help me organize my codebase in the long run. I will finally be able to go with the proper timing analysis of my functions and break the big chunk of functions into smaller efficient modules.Learning a lot from all this.

Enjoying it a lot. Python is really great.

Automating tasks in Python – Running many Python Scripts, running scripts at startup

I started my automation by making a script for cleaning up my C code’s directory. I then thought what would happen if I had more than one scripts for automation? I thought to make a script for running other Python scripts.

import sys
import subprocess

theproc = subprocess.Popen([sys.executable, 
"Deleting extra"])

This simple script can be used to run a script in the current directory. Here sys.executable is for the Python exe present on the computer and the second string in the list is for the script that you want to execute. You can use an absolute path to run scripts in other directories also.

To make a Python script run at Windows Startup you need to find the startup folder in your computer and place a batch file in it. A google search tell you where it is for your version of Windows.

Use notepad++ to make  a .bat file(Just use Save as and select .bat instead of .txt). In that file write

python "<Path to you Python file>/<name_of_python_script>.py"

Here you have to write the ” on either side as I have written. That makes it possible for you to just copy and paste the path to your scripts.

Automating tasks in Python – Deleting files

I have decided to use Python for automating some tasks that I do regularly and which consume a lot of time.

I have started with deleting useless files from my code base. I have been using github as a backup for my codebase. I need to upload the files on github server. When I am programming in C many executables and object files(.exe and .o) are generated. I need to delete these everytime before I commit my code base to github because backing up these files is of no use to me.

My git repo containing the full script can be viewed here. I’ll keep it updated there. I am placing the code here also but no updates guaranteed here.

import os

cur = os.getcwd() + "\\..\\..\\..\\git_repo\\C\\"
extensions = ['.exe', '.o']
exclude = ['.git']

def delete_or_not(name):
    for i in extensions:
        if name.endswith(i):
            return True

    return False

def cur_files(files):
    for name in files:
        if delete_or_not(name):
            yield name

def main(startpath):
    for path, folders, files in os.walk(startpath, True):
        for folder in exclude:
            if folder in folders:

        for name in cur_files(files):
            os.remove(path + "\\" + name)main(cur)

The script is simple. The things that anyone would need to change are at the beginning. These are cur, extensions and exclude.

  1. cur is the path of the folder, in which you start deleting files, relative to the script. If you want to put absolute path you need to take out os.getcwd(). Files in its subfolders are also deleted.
  2. extensions are the file extensions which you want to delete.
  3. exclude are the folder which you want to exclude from this deletion script’s scope. Generally you would want the hidden folders to be excluded. I included one folder which I did not want the script to go through.

After making these changes you can simply double click to delete files if you have Python installed. I use Python 2 but this should be easy to convert to Python 3 also.

Next time I’ll make a script to run a group of Python scripts. That’ll help me to make any number of different scripts and choose what I need to run at different times like at startup, shutdown, before uploading files etc.

My experience with Python and C so far

I started Python on 18th May 2013 after I discovered that doing project euler problems via C(my primary language) wasn’t easy for me. Using Python doing those problems were quite easy. I started neglecting C and went ahead with Python with zeal. That has been helpful as using Python I have been able to solve 59 problems on project euler so far.

My project Euler progress so far.

Using C I went ahead to solving problems 11 and then felt discouraged. Maybe I wasn’t focusing on the logic so much.

By Python I have been able to do some things that I couldn’t do using C. Like making solutions easily to some problems. Using scripting in Python I found out that looking into files, copying them and many other things can be extremely easy.

  • I wrote scripts for finding whether I had done a particular thing in C but not in Python and vice versa. I arranged the files in folders for using this script but organizing code is always nice.  What was taking me a lot of time manually now can be done by a simple double-click now.
  • Writing a lot of functions in a single file was starting to be confusing. I wrote up a script for finding the function signatures. Took out a lot of confusion.
  • Check my github repo if you want to have a look at any of these scripts/

I am thinking about breaking down the file containing all the functions into many different files. That is the only way to go forward if I want to keep writing. Then I am thinking of making a GUI that I can use to separate all the functions by tags. I don’t know much how to do that as of now but I think I’ll use wxPython but that isn’t final.

I found that there are always a lot of new things that can be learned in programming and that stackoverflow can be a great resource. Check out my list of favourite questions and my own questions on stackoverflow. Maybe you’ll find something that you are looking for.

I have found out that using online judging can be a great thing. I have been using codechef for practicing C and Python. By using this I discovered that I will need to learn many algorithms and that there a lot more things to learn in any programming language that I choose to learn. It has been really helpful and the community is good. I started reading other people’s code thanks to codechef and stackoverflow. It has been helpful to me. I discovered that I can discover many different ways of doing things by looking at other people’s code. I initially joined github for the the same reason – looking at other people’s code. But I discovered that looking at other people’s code also helps if there is a large vvariety of code and their time and memory usage is available.

I  tried spoj for 2-3 days. I think there are more problems for practice than on codechef but I will need to learn more algorithms before I seriously try to solve them.

Solving codechef’s problems I discovered that the simple program of factorial that I usually wrote in C wouldn’t work even for factorials of 100. I later found out a way to calculate factorials of any number. It will be needing some polishing and final touches but I found a way.

I have placed Python on hold for past few days. I use it now and then but I am shifting focus back on C. I have started the MIT’s course Introduction to algorithms whose I have placed with a lot of other programming links. It is nice. I have finally overcome my fear of sorting algorithms. They are kind-of-basic things in programming but I couldn’t do them properly. While using linked lists, stacks etc. was something that I could do sorting was difficult for me. But now I am able to write insertion sort, selection sort, bubble sort and merge sort easily. Reading Cormen’s Book of algorithm is a challenge that I have taken up. It is more than 1300 pages book so it is a huge challenge but I believe that it can bridge a lot of gaps in my knowledge.

Using codereview is helpful. It helped me in changing a lot of formatting problems and bad coding practices that I was using. I have decided to get code reviewed for all algorithms that I use. Writing review is helpful. Looking up someone’s code and trying to give suggestions is good as experience.

I have some things to do this week. I’ll try to do programming in spare time. I’ll keep my focus on C and learning algorithms. I believe it would be helpful in the long run.