Вы можете взять входное имя файла, разделить его, чтобы получить часть перед периодом, а затем добавить / добавить «output» и «.txt»:
xmlfile = "ab123.xml"
txtfile = "output" + xmlfile.split(".")[0] + ".txt"
print(txtfile)
Выход:
outputab123.txt
Итак, ваш код может выглядеть примерно так:
listofiles = # define list of files here (eg. using glob)
for xmlfile in listoffiles:
# parsing here
parser.parse(xmlfile)
sys.stdout.close()
txtfile = "output" + xmlfile.split(".")[0] + ".txt"
sys.stdout = open(txtfile, encoding="UTF-8")
# write to text file here
Чтобы получить список .xml
файлов в каталоге, вы можете использовать glob :
listoffiles = glob.glob("/path/to/directory/*.xml")