Код: Выделить всё
2_Product
2-1_CategoryName1_Product
2-1-1_Name1_Product
LLL_nomenclature1_product.zip
LLL_nomenclature1_product (folder)
notice_nomenclature1.pdf
LLL_nomenclature1_product_metadata.xml
LLL_nomenclature2_product.zip
LLL_nomenclature2_product (folder)
notice_nomenclature2.pdf
LLL_nomenclature2_product_metadata.xml
LLL_nomenclature3_product.zip
LLL_nomenclature3_subproduct1 (folder)
notice_nomenclature3.pdf
LLL_nomenclature3_subproduct2 (folder)
notice_nomenclature3.pdf
LLL_nomenclature3_subproduct3 (folder)
notice_nomenclature3.pdf
LLL_nomenclature3_product_metadata.xml
... etc
2-1-2_Name2_Product
2-1-3_ ...etc
2-2_CategoryName2_Product
2-2-1_ ...
2-2-2_ ...
... etc
Код: Выделить всё
import os
import xml.etree.ElementTree as ET
import zipfile
for root, dirs, files in os.walk("."):
for folder_ext in files:
if folder_ext[-4:] == '.zip' and folder_ext[:3] == 'LLL':
filePath3 = os.path.join(root, folder_ext)
zip_folder = zipfile.ZipFile(filePath3)
zipfile_paths = zip_folder.namelist()
for paths in zipfile_paths:
zipfiles = os.path.basename(paths)
if zipfiles[-4:] == '.pdf' and zipfiles[:3] == 'not':
notice_name = zipfiles
for prdt in files:
if prdt[-4:] == '.xml' and prdt[:-13] == folder_ext[:-4] :
filePath4 = os.path.join(root, prdt)
xml_produit = ET.parse(filePath4)
root_produit = xml_produit.getroot()
notice_tag = ET.SubElement(root_produit, "notice_pdf")
notice_tag.text = notice_name
ET.indent(root_produit)
xml_produit.write(filePath4, encoding='utf-8', xml_declaration=True, method='xml', short_empty_elements=False)
Код: Выделить всё
.
.
.
notice_nomenclature1.pdf
Код: Выделить всё
.
.
.
notice_nomenclature3.pdf
notice_nomenclature3.pdf
notice_nomenclature3.pdf
Я пробовал использовать .sort() и сортировать, но безрезультатно.
И я пробовал это:
Код: Выделить всё
...
new_list = []
for paths in zipfile_paths:
zipfiles = os.path.basename(paths)
if zipfiles[-4:] == '.pdf' and zipfiles[:3] == 'not':
if zipfiles not in new_list:
new_list.append(zipfiles)
notice_name = new_list
... etc
Код: Выделить всё
TypeError: write() argument must be str, not list
Спасибо.
Подробнее здесь: https://stackoverflow.com/questions/786 ... ist-and-us