Читаем Приложение к «Python в библиотеке» полностью

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])

, , , , , , , ,Books.pop(i-1)

, , , , , , else:

, , , , , , , , mPrintList(old_name, L)

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

, , , , , , , , flag = 0

, , , , else:

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

, ,

, ,if flag == 1:

, , , ,mPrintList(old_name, L)

, , , ,

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

def mMain():

mySorting()

fbName = "f"

fNum = 1

fName = fbName + str(fNum) #'f1'

num = 0

if len(Books) > 0:

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

, , , , os.mkdir(fName)

, , for i in Books:

, , , , if (not os.path.isfile(path+"/"+fName)):

, , , , , , shutil.move(i[1], path+"/"+fName)

, , , , num += 1

, , , , if num == 9: # number in folder !!!

, , , , , , fNum += 1

, , , , , , fName = fbName + str(fNum)

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

, , , , , , , , os.mkdir(fName)

, , , , , , num = 0

print( 'Done!')

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

parse_dir(path)

if len(Books) == 0:

, ,print('Empty dir')

else:

, ,mMain()

6

fb2err4.py

#!/usr/bin/env python

# -*- codning: utf-8 -*-

import sys, os

# проверка структуры файла fb2

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

file = 'lib.fb2'

#count = 1 # счетчик строк

flag = False # признак анализа тега

S = '' # место для обработанного тега

Tag = '' # место для тега

L = [] # стек

Tagss = []

'''

with open(file, encoding="utf8") as f:

, ,LList = f.readlines()

for i in LList:

, ,count += 1

, ,if i.find('section') > -1:

, , , ,print(count)

, , , ,print(i)

f.close()

'''

def printStack():

, ,st = open("stack.txt", "w")

, ,for n in Tagss:

, , , ,#print(n) # вывод на этран

Перейти на страницу:

Похожие книги

Programming with POSIX® Threads
Programming with POSIX® Threads

With this practical book, you will attain a solid understanding of threads and will discover how to put this powerful mode of programming to work in real-world applications. The primary advantage of threaded programming is that it enables your applications to accomplish more than one task at the same time by using the number-crunching power of multiprocessor parallelism and by automatically exploiting I/O concurrency in your code, even on a single processor machine. The result: applications that are faster, more responsive to users, and often easier to maintain. Threaded programming is particularly well suited to network programming where it helps alleviate the bottleneck of slow network I/O. This book offers an in-depth description of the IEEE operating system interface standard, POSIX (Portable Operating System Interface) threads, commonly called Pthreads. Written for experienced C programmers, but assuming no previous knowledge of threads, the book explains basic concepts such as asynchronous programming, the lifecycle of a thread, and synchronization. You then move to more advanced topics such as attributes objects, thread-specific data, and realtime scheduling. An entire chapter is devoted to "real code," with a look at barriers, read/write locks, the work queue manager, and how to utilize existing libraries. In addition, the book tackles one of the thorniest problems faced by thread programmers-debugging-with valuable suggestions on how to avoid code errors and performance problems from the outset. Numerous annotated examples are used to illustrate real-world concepts. A Pthreads mini-reference and a look at future standardization are also included.

David Butenhof

Программирование, программы, базы данных