Posts

Showing posts from May, 2008

List of files, directories in Python

Here I present a short python program that prints the list of all files inside the current directory and subdirectories. It prints filename with relative path to the current directory. import os for root, dirs, files in os.walk('./'): for name in files: filename = os.path.join(root, name) print filename You can also print the list of directories only (just print the dirs). Actually I needed this stuff to solve a problem from Google treasure hunt . Let me know if you want me to share the full source code of the solutions of Google treasure hunt. Also share your code, if you have any different idea! Check the python documentation to learn more about processing Files and Directories.

Myspace auto login using Python

Today I am going to share with you an interesting program - a Python script that logs in to myspace given your username (email address) and password. If you study the script, you will get idea about the following things: HTTP POST in Python How to use cookiejar How to add user-agent, referer etc. to the request header using Python And of course, how to auto login to a site :) Here is the code: import urllib2, cookielib, re url = 'http://secure.myspace.com/index.cfm?fuseaction=login.process' email = "" #put your email address here pwd = "" #put your password here data = "__VIEWSTATE=%2FwEPDwUKLTY1Mjc2MTMwMWQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgIFPWN0bDAwJGN0bDAwJE1haW4kY3BNYWluJFNwbGFzaERpc3BsYXkkY3RsMDAkUmVtZW1iZXJfQ2hlY2tib3gFPWN0bDAwJGN0bDAwJE1haW4kY3BNYWluJFNwbGFzaERpc3BsYXkkY3RsMDAkTG9naW5fSW1hZ2VCdXR0b24%3D&NextPage=&ctl00%24ctl00%24Main%24cpMain%24SplashDisplay%24ctl00%24Email_Textbox="+email+"&ctl00%24ctl00%24Mai

First problem I faced in PyS60

When I tried to connect to a website using Python from my mobile phone, I got into trouble. Every time I tried a simple program I got the following error message: IOError:[Errno socket error](0, 'getaddrinfo failed') Here is the code I tried: import urllib usock = urllib.urlopen("http://example.com") data = usock.read() usock.close() I tried Google but couldn't find any good solution. Then I did some trial-n-error myself and got rid of the problem! Actually when I connect to Internet from my mobile phone I have 3 access points - GP-WAP, GP-MMS and GP-Internet. When I browse from my mobile phone (it's Nokia N70), I use GP-WAP. So when the python program asked for the option I selected that and got the error. When I selected GP-Internet, I got rid of the problem. Now I can connect the Internet using Python from mobile!!!

py60 - Python for S60

Yes, I have started using Python in my mobile phone !!! Python for S60 is Nokia's port of the Python language to the S60 smartphone platform. In addition to the standard features of the Python language, PyS60 provides access to many of the phone's uniquely smartphone-y functions, such as camera, contacts, calendar, audio recording and playing, TCP/IP and Bluetooth communications and simple telephony. (from http://wiki.opensource.nokia.com/projects/Python_for_S60 ) You can use emulator or your mobile phone (S60 platform) to run/test python programs. I use my Nokia N70 to run python and so far it's great fun! You can write the program in your computer and transfer it to your mobile phone. There is an interactive console in the mobile phone so that you can write your program directly in the mobile phone. There is another blue-tooth based console which I haven't tried yet. You can download the latest version of pyS60 from sourceforge . And I am using this tutorial in my p

Common problem in CSV file writing

First let me tell you how I write CSV files :) I don't use Pythons CSV module for writing. I just name the file xyz.csv (fp = open('xzy.csv', 'w')) and write using something like: fp.write(a+','+b+','+c) But sometimes it can create a problem if any string contains a comma (,) within itself. For example if the string a is 'hello, how are you?', then it will be printed in two columns... so before writing to file I use: a = a.replace(',', ' ') Is there any other way to avoid such situation? If you know, please share.

Convert hex to ASCII string in Python

Sometimes web pages use hexadecimal values of of ASCII characters. To parse them we need to convert them to ASCII string first. Today I faced a similar problem and came up with the following solution: import binascii, re def asciirepl(match): # replace the hexadecimal characters with ascii characters s = match.group() return binascii.unhexlify(s) def reformat_content(data): p = re.compile(r'\\x(\w{2})') return p.sub(asciirepl, data) hex_string = '\x70f=l\x26hl=en\x26geocode=\x26q\x3c' ascii_string = reformat_content(hex_string) print ascii_string I took help of binascii module (and of course the re module). The logic was simple, I just replaced \x.. (here .. represents two characters) with their corresponding ASCII characters. You can check my other post to convert ASCII values to ASCII characters: http://love-python.blogspot.com/2008/04/get-ascii-string-for-decimal-number-in.html And please share if you have any other idea.

generate different file name each time you run a program

I have written a script today which has an interesting behavior. Each time the program runs, it generates output in a different file (or in other word, the file name has to be different each time the program runs). So I decided to use current time as the prefix of the filename. I used the localtime() function of time module in Python. >>> import time >>> time.localtime() (2008, 5, 9, 23, 32, 8, 4, 130, 0) >>> I made a string from localtime and added it before the filename. You can get the idea from the following code block. It's so simple! import time prefix = '' for t in time.localtime(): prefix += t.__str__() fileName = prefix + "_result.csv" print fileName Keep enjoying Python!

getpass - Python module for portable password input

Last night I found an interesting Python module, getpass . It has two functions: getpass (): Prompt the user for a password without echoing. getuser (): Return the login name of the user. The getpass function was really useful for me. But sometimes you would want to avoid the getuser function, and provide user name on your own. Please check the Python documentation for details: http://docs.python.org/lib/module-getpass.html