Use __repr__ for formatting file instead of __str__ because non-standard characters will be escaped and the string will be properly quoted. Also, a modules __file__ is not *guaranteed* to be a string (although it almost always is), nevertheless, __repr__ generally results more more humanly readable results. Note that __repr__ was already used to format __file__ in most places.