I have 2 raster files of different resolution (having different pixel size and origin, but both files are georeferenced). I want to align the 2 files so that each pixel of first raster coincides with the corresponding pixel of second raster. In QGIS, this can be done using the align tool, but I wish to write a script in python for this purpose.
Select which image you want to use as reference and check the pixel size of that with gdalinfo. Then warp the second image to have the same pixel size with gdalwarp http://www.gdal.org/gdalwarp.html by using both -tr (target resolution) and -tap (target aligned pixels) options.
If the origin of the reference image does not match with the pixel size you must align also that with gdalwarp and -tap. Example: if pixel size is 2 meters and origin is at (500000,58002) then the pixels are aligned but if the origin is at (500000,58001) then they are not aligned.
With Python you can either call the gdalwarp executable or use warp as a library function directly in Python http://erouault.blogspot.fi/2015/10/gdal-and-ogr-utilities-as-library.html.