You needn't to write a torrc file for each relay. You can launch tor passing arguments dynamically through stem. For instance:
import stem.process
tor_process = stem.process.launch_tor_with_config(
tor_cmd = PATH_TO_TOR_LAUNCHER,
config = {
'SOCKSPort': str(SOCKS_PORT),
'ControlPort': str(CTRL_PORT),
'DataDirectory': DATA_DIR,
'GeoIPFile': DATA_DIR + 'geoip',
'GeoIPv6File': DATA_DIR + 'geoip6',
'ExitNodes': '{es},{pt}' # exiting through Spain or Portugal
}
)
These are the country codes from the database for geolocation that Tor uses by default: http://dev.maxmind.com/geoip/legacy/codes/iso3166/