I think there is a cleaner way to do this. How about `filename_1 = glob.glob('%s%s???%d.xvg.all' % (dir, res_name1, res_id1))` ?