Extracts marks of all students in class from website This code extracts marks of all students in class and stores the result in a file results.txt, using BeautifulSoup. I'm looking for code review and suggestions.

from bs4 import BeautifulSoup
import requests

file = open("results.txt","w")

for roll in range(1,60):
    if roll in range(1,10):
            rollstr = '0'+str(roll)
    else:
            rollstr = str(roll)
    url = 'http://www.*.org/'
    r = requests.get(url)

    data = r.text
    soup = BeautifulSoup(data)

    file.write('roll: ' +rollstr + ' ')

    for i in soup.find_all('strong'):
            sgpa = i.get_text()
            if sgpa.find('SGPA') == 0:
                    file.write(sgpa)
                    break
    file.write('\n')
Style

Your code looks good but you might like to know that there is a Style Guide for Python Code called PEP 8. If you are interested, you'll find tools to check your code automatically (pep8) and to fix it (autopep8).

In your case, I can see straight-away that you are sometimes missing spaces around your '+' operator.

String format

You'll find various more pythonic ways to format your number with leadings 0s.

Also, in case you want to keep your current logic, comparison can be written pretty succintly in Python so that you can simply write : if 1 &lt;= roll &lt; 10. (Please note that in Python 2.x, range returns a list so checking if x in range(whatever) is not efficient in time (it has to check all elements if the elements you are looking for is not in the list) nor in memory (a list is built for no reason)).

Using context managers

Opening files and remembering to close them is a bit tedious. Good thing is that Python has a clean solution to this : the with keyword. In a nutshell, you use with when you open your file, you add a level of indentation and the magic to close your file will happen automaticaly.