This will work in a pinch for simple values that you want to add to a query via .format() or string interpolation:
escape = lambda value: psycopg2.extensions.QuotedString(str(value)).getquoted()
Manyally wrap values you're concerned about with escape(), as in
sql_query.format(my_filename=escape(my_filename))
--David