Zlib,bz2 and Gzip compression in Python..

Zlib compression and Decompression in Python
a=”ASDADASDASDASDASD”
import zlib
c = zlib.compress(a)
#to decompress
d=zlib.decompress(c)

//Bz2 Compression and Decompression in Python
import bz2
c =bz2.compress(a)
#to decompress
d=bz2.decompress(c)



Base64 encoding and Decoding in Python…

 Base64 encoding
import base64
page=”spmethingsadasd”
a=base64.b64encode(the_page)
print a
Base64 decoding in Python:
import base64
b=base64.b64decode(a)
print b



Python code to index a site…

#!C:/Python25/python.exe -u
import MySQLdb
print “Content-type: text/html”
db=MySQLdb.connect(host=”localhost”,user=”xxxxx”,passwd=”xxxxxx”,db=”xxxxx”)
cursor=db.cursor()
import socket
timeout=1
a=1
socket.setdefaulttimeout(timeout)
from urllib2 import Request, urlopen, URLError, HTTPError
cursor.execute(’SELECT * FROM `stringf` LIMIT 20,5′)
numrows = int(cursor.rowcount)
# get and display one row at a time
x=20
for x in range(20,25):
row = cursor.fetchone()
print row[0], “–>”, row[1]
url=’http://www.’+row[1]+”
req = Request(url)
try:
response = urlopen(req)
except:
print ‘error’
else:
try:
the_page=response.read()
b=row[1]+’.txt’
f=open(b,’w’)
f.write(the_page)
f.close()
except:
print ‘error’



Writing contents to file in Python

Source code for writing contents to file in Python
file=open(’2.txt’,’w’)
file.write(’asdasdadas’)
file.close



Reading a file in Python

Source code for reading a file in Python:
file=open(’2.txt’,’r’)
file1=file.read()
file.close
to see the contents of the read  file simply use
print file1



Creating folders/directories in Python

Below is the code which will allow you to create folders/directories in Python
import os
os.makedirs(’C:/Python25/testing/a/dddf/’,mode=666)
This will create a directory called dddf under a and it will have permissions 666