Приложение к «Python в библиотеке» - страница 2

стр.

# , ,print fn+'.zip'

, ,z = zipfile.ZipFile(fn+'.zip', 'w', zipfile.ZIP_DEFLATED) # Создание нового архива

, ,z.write(fn)

, ,z.close()

, ,os.remove(FileName)

, ,co += 1

def parse_dir(fn):

, , , ,m = fn.split('.')[-1]

# , , , ,print fn

, , , ,if (m == 'fb2'):

, , , , , ,parse_file(fn)

, , , , , ,

, , , , , ,

path = os.getcwd()

files = os.listdir(path)

for file in files:

# , , , ,print (os.path.join(path, file))

, , , ,parse_dir(os.path.join(path, file))

print ('Zip => ' + str(co))

print ('Done!')

5

main_ok.py

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import sys, os

import xml.dom.minidom

import zipfile

import shutil

# form 21.05.2021

#sys.path.append("/work/Python/modules/s")

#--------------------------------------------------

#from m_list import Books

Books = []

def FindValue(s1, s2):

global Books

# L1 = []

maxW = len(Books)

L1 = [s1, s2]

if maxW == 0:

, , Books.append(L1)

# , ,Books += [s1, s2]

else:

, , minW = -1

, , cur = maxW // 2

, , wList = Books[cur][0]

, , while not(s1 == wList):

, , , ,if s1 < wList:

, , , , , ,maxW = cur

, , , ,else:

, , , , , ,minW = cur

, , , ,if maxW - minW == 1:

, , , , , ,if s1 > wList:

, , , , , , , ,cur += 1

, , , , , ,Books.insert(cur, L1)

, , , , , ,return

, , , ,cur = ((maxW-minW) //2) + minW

, , , ,wList = Books[cur][0]

, , Books.insert(cur+1, L1)

def PrintList():

, ,for i in Books:

, , , ,print (i[0] + ' ' + i[1])

def SaveList():

, ,f = open('lib.txt', 'w')

, ,for i in Books:

, , , ,f.write(i[0].encode('cp1251')+'\n')

, , , ,f.write(i[1]+'\n\n')

, ,f.close()

#--------------------------------------------------

#from m_dir import parse_dir

Capture = ''

FileName = ''

def parse_zip(fn):

, ,global FileName

# , ,print >> sys.stderr, 'Zip:', os.path.basename(fn)

, ,FileName = fn

, ,z = zipfile.ZipFile(fn, 'r')

, ,filelist = z.namelist()

, ,filelist.sort()

, ,for n in filelist:

, , , ,try:

, , , , , ,if n[-4:] == ".fb2":

, , , , , , , ,parse_fb2(z.open(n))

, , , ,except:

# , , , , , ,print >> sys.stderr, 'X15:', n

, , , , , ,print( "X15:", n )

def parse_fb2(fn):

, ,global Capture

, ,if isinstance(fn, str):

, , , ,fn = open(fn)

, ,try:

, , , ,dom = xml.dom.minidom.parse(fn)

, ,except:

, , , ,print('Error:')

, , , ,print(FileName)

, ,else:

, , , ,dom.normalize()

, , , ,node1=dom.getElementsByTagName("description")[0]

, , , ,node1=node1.getElementsByTagName("title-info")[0]

, , , ,try:

, , , , , ,node1=node1.getElementsByTagName("author")[0]

, , , , , ,node1=node1.getElementsByTagName("last-name")[0]

, , , , , ,s = node1.childNodes[0].nodeValue

, , , , , ,s = s.encode("utf-8")

, , , , , ,Capture = s.capitalize()

, , , ,except:

, , , , , ,Capture = "noname"

, , , ,FindValue(Capture, FileName)

def parse_file(fn):

, ,global FileName

, ,FileName = fn

, ,m = fn.split(".")[-1]

, ,if (m == "zip"):

, , , ,parse_zip(fn)

, ,elif (m == "fb2"):

, , , ,parse_fb2(fn)

, , , ,

def parse_dir(fn):

, ,dirlist = os.listdir(fn)

, ,dirlist.sort()

, ,for i in dirlist:

, , , ,if os.path.getsize(i) > 0:

, , , , , ,parse_file(os.path.join(fn, i))

#? , , , ,else:

#? , , , , , ,print("bad zip "+ i)

#--------------------------------------------------

#from sort import path, mySorting

path = os.getcwd()

def mPrintList(oldn, M):

, ,global path

, ,ss = str(oldn, encoding='utf-8')

# , ,ss = path+"/"+s.encode("utf-8")

# , ,print(s)

, ,if (not os.path.isdir(ss)):

# , , , ,print(ss)

, , , ,os.mkdir(ss)

, ,for k in M:

, , , ,if (not os.path.isfile(ss)):

# , , , , , ,print(ss)

, , , , , ,

, , , , , ,shutil.move(k, ss)

# , , , ,print(ss)

# , , , ,shutil.move(k, ss)

, , , ,

def mySorting():

, ,global Books

, ,old_name = Books[len(Books)-1][0]

, ,flag = 0

, ,for i in range(len(Books)-1, 0, -1):

, , , , if flag == 0 and Books[i-1][0] == old_name:

, , , , , , flag = 1

, , , , , , L = []

, , , , , , L.append(Books[i][1])

, , , , , , Books.pop(i)

, , , , , ,

, , , , if flag == 1:

, , , , , , if Books[i-1][0] == old_name:

, , , , , , , ,L.append(Books[i-1][1])