Saturday, January 21, 2012

Python Note 8 (Python Utilities)

Summary
filenames = os.listdir(dir) -- list of filenames in that directory path (not including . and ..). The filenames are just the names in the directory, not their absolute paths.
import os

dir="c:/"
files=os.listdir(dir)
for path in files:
    print(path)
                 
Output :
addins
AppCompat
AppPatch
assembly
avastSS.scr
bfsvc.exe




os.path.join(dir, filename) -- given a filename from the above list, use this to put the dir and filename together to make a path
import os

mydir="c:/"
files=os.listdir(mydir)
for filename in files:
    print(os.path.join(mydir,filename))
                 
Output :
c:/$Recycle.Bin
c:/.rnd
c:/Documents and Settings
c:/EclipseWorkSpace
c:/eclipseWorkspaceJee

os.path.abspath(path) -- given a path, return an absolute form, e.g. /home/nick/foo/bar.html
import os

mydir="c:/"
files=os.listdir(mydir)
for filename in files:
    print(os.path.abspath(os.path.join(mydir,filename)))
Output :
c:\$Recycle.Bin
c:\.rnd
c:\Documents and Settings
c:\EclipseWorkSpace
c:\eclipseWorkspaceJee
c:\eclipse_jee

os.path.dirname(path), os.path.basename(path) -- given dir/foo/bar.html, return the dirname "dir/foo" and basename "bar.html"

os.path.exists(path) -- true if it exists
os.mkdir(dir_path) -- makes one dir, os.makedirs(dir_path) makes all the needed dirs in this path
import os

print(os.path.dirname("c:/EclipseWorkSpace/bin"))
print(os.path.exists("c:/windows"))
try :
    x=os.makedirs("c:/a/b/c/d/e/f/g/h") #creates all directories
    print("All directories created successfully")
except WindowsError :
    print("Directories are already exsists!")
Output :
c:/EclipseWorkSpace
True
All directories created successfully

shutil.copy(source-path, dest-path) -- copy a file (dest path directories should exist)
import shutil

try :
    shutil.copy("D:/jython.jar","d:/J/jython.jar")
    print("File copied successfully!")
except IOError :
    print("Permission denied!")
  • Using urllib to create http requests
in python 3 some urllib functions don't backward compatible. so we can't use some old http request methods
  • Reading a http resource fully or partially
Code :
import urllib.request
f = urllib.request.urlopen('http://python.org')
info=f.info()
ctype=info['Content-Type'] #identify content type
print("Info : {}".format(info))
print("Type : {}".format(ctype))
if "text/html" in ctype : #check for a content type
    print(f.read().decode('utf-8')) #read all ,read(x) means read x bytes
else :
    print("Content type {} is not supported!".format(ctype))
f.close()
Result :
Info : Date: Sun, 22 Jan 2012 03:47:52 GMT
Server: Apache/2.2.16 (Debian)
Last-Modified: Sat, 21 Jan 2012 00:32:28 GMT
ETag: "105800d-49c2-4b6feee9c1f00"
Accept-Ranges: bytes
Content-Length: 18882
Connection: close
Content-Type: text/html
X-Pad: avoid browser bug


Type : text/html
<!DOCTYPE html PUBLIC "-//W3C//DTD XH.................
  • Using custom headers in the request
Code :
import urllib.request
req=urllib.request.Request("http://google.lk")
req.add_header ('Referer','http://python.org')
req.add_header ('User-agent','kanibrowser/1.0beta')
handl=urllib.request.urlopen(req)
text=handl.read().decode('utf-8') #read the server response
  • GET Method
Code 1 :
import urllib.request
import urllib.parse

params=urllib.parse.urlencode({'Name':'Kanishka','Age': 23,'ID':'2314'})
handl=urllib.request.urlopen("http://mysite.net/cgi-bin/query?%s" % params)
response=handl.read().decode('utf-8')
print(response)
handl.close()

Code 2: Google search using GET request
import urllib.request
import urllib.parse

params=urllib.parse.urlencode({'q':'Python Language History','ie':'utf-8'})
handl=urllib.request.urlopen("http://google.lk/search?%s" % params )
response=handl.read().decode('utf-8')
print(response)
handl.close()
  • POST Method
Code :
import urllib.request
import urllib.parse

params=urllib.parse.urlencode({'Name':'Kanishka','Age': 23,'ID' : '2314'})
params=params.encode('utf-8')
handl=urllib.request.urlopen("http://mysite.net/cgi-bin/query",params)
response=handl.read().decode('utf-8')
print(response)
handl.close()
  • Specify proxies
We can explicitly specify a proxy instead of environment string
Code :
import urllib.request
proxies={'http':'http://192.168.4.5:8080/'}
opener=urllib.request.FancyURLopener(proxies) #surf with given proxies
handlr=opener.open("http://google.in")
response=handlr.read().decode('utf-8')
print(response)



0 comments:

Post a Comment

© kani.stack.notez 2012 | Blogger Template by Enny Law - Ngetik Dot Com - Nulis