First of all, download the rarfile.py from http://grue.l-t.ee/~marko/src/rarfile/README.html. This python program contains all the classes and methods to handle a .rar file. Download it and save it in the LIB folder.
# check if it is rarfile where f is a file
# if it is a rar file then open the file for reading
z = rarfile.RarFile(f, r)
# get all the filenames in the .rar package
for filename in z.namelist():
In this way you can manipulate a rar file. In the same way you can manipulate a tar file or a zip file by using the zipfile class or tar file class in python. But in tarfile class we have the getnames() class object we should be using instead of nameslist() class object.