Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #-*- coding: utf-8 -*-
- from lxml import etree, html
- from pathlib import Path
- import os, shutil
- import logging
- logger = logging.getLogger()
- logger.setLevel(logging.DEBUG)
- formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
- fh = logging.FileHandler('log.txt')
- fh.setLevel(logging.DEBUG)
- fh.setFormatter(formatter)
- logger.addHandler(fh)
- ch = logging.StreamHandler()
- ch.setLevel(logging.DEBUG)
- ch.setFormatter(formatter)
- logger.addHandler(ch)
- logger.info('-------------------')
- logger.info('Программа запущена.')
- def parsing_xml(path_to_xml_file, name_xml_file):
- try:
- with open(path_to_xml_file + name_xml_file, 'r') as f:
- xml = f.read().encode('utf-8')
- except IOError as e:
- logger.error('В директории ' + path_to_xml_file + ' отсутствует файл ' + name_xml_file)
- else:
- list_from_xml = etree.XML(xml)
- index = 0
- for i in list_from_xml:
- if list_from_xml[index].tag == 'ProductID':
- p_id = list_from_xml[index].text.replace(' ','')
- index += 1
- return p_id
- def get_part_for_comparison(string):
- splitter = []
- for i in string:
- if i == '_':
- break
- splitter.append(i)
- compare = ''.join(splitter)
- return compare
- def create_destination_directory(prod_id, destination_dir):
- if prod_id in os.listdir(destination_dir):
- logger.info('Директория' + prod_id + ' уже есть.')
- else:
- logger.info('Директории ' + prod_id + ' нет. Создаю.')
- os.mkdir(prod_id, mode=0o777)
- logger.info('Директория ' + prod_id + ' создана.')
- pass
- def main(source_dir, destination_dir, name_xml_file):
- #получаем список всех директорий
- for d, dirs, files in os.walk(source_dir):
- for i in dirs:
- path = source_dir + i + '/'
- if get_part_for_comparison(i) == 'PhotoBook':
- if os.listdir(path):
- if not os.path.exists(path + name_xml_file):
- continue
- prod_id = parsing_xml(path, name_xml_file)
- os.chdir(destination_dir)
- create_destination_directory(prod_id, destination_dir)
- shutil.move(source_dir + i, destination_dir + prod_id)
- logger.info('Заказ ' + i + ' перенесен!')
- else:
- logger.warning('Директория ' + path + ' пуста.')
- pass
- name_xml_file = 'PDF.xml'
- home_dir = str(Path.home())
- source_dir = home_dir + '/cur/test/'
- destination_dir = home_dir + '/cur/sorted/'
- main(source_dir, destination_dir, name_xml_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement