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

, , , ,file=os.path.join(sSrc,file)

, , , ,if os.path.isdir(file):

, , , , , ,parse_dir(file)

, , , ,else:

, , , , , ,m = file.split('.')[-1] # извлечение расширения

, , , , , ,m = m.lower()

, , , , , ,if (m == ex):

, , , , , , , ,parse_file(file)

, , , , , , , ,

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

path = os.getcwd()

parse_dir(path)

GFiles.sort()

ScanSize(GFiles) #

if co > 0:

f = open('merg_'+ex+'.txt', 'w')

for i in LOut:

, , f.write(i+'\n')

f.close()

print('Done '+ex+' -> '+str(co))

same_fb2.py

#!/bin/env python

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

import sys, os

from m_list import FindValue, PrintList, Books

list = []

def fb2zip(pa):

, ,p = pa.split('/')

, ,FindValue(p[-1],pa)

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

def GetListFiles(PathForWork):

, ,global list

, ,for file in os.listdir(PathForWork):

, , , ,path = os.path.join(PathForWork, file)

, , , ,ex = path.split('.')

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

, , , , , ,if ex[-1].lower() == 'fb2':

, , , , , , , ,fb2zip(path)

, , , , , ,else:

, , , , , , , ,e = ex[-2]+ex[-1]

, , , , , , , ,if e.lower() == 'fb2zip':

, , , , , , , , , ,fb2zip(path)

, , , ,else: #

, , , , , ,GetListFiles(path) #

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

def CompareList():

, ,OldFile = ''

, ,OldPath = ''

, ,flag = False

, ,for it in Books:

, , , ,if (it[0] == OldFile):

, , , , , ,if not flag :

, , , , , , , ,flag = True

, , , , , , , ,list.append(OldPath)

, , , , , ,list.append(it[1])

, , , ,else:

, , , , , ,if flag:

, , , , , , , ,flag = False

, , , , , , , ,list.append('------------')

, , , , , ,OldFile = it[0]

, , , , , ,OldPath = it[1]

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

# Main:

Mpath = os.getcwd()

GetListFiles(Mpath)

CompareList()

if len(list) == 0:

, ,print('Дубликатов нет.')

else:

, ,new_file = open(fn2,'w')

, ,for i in list: #

, , , ,new_file.write(i)

, , , ,print i

, ,print 'Done!'

, ,new_file.close

scan7i.py

#!/usr/bin/env python

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

import sys, os

import zipfile

import datetime

import xml.dom.minidom

# 7 avg 2021

# Запись списка книг в "небазу" (третья,2 версия)

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

Books = []

Errror = []

siz = 0 # размер файла

countFiles = 0

book_title = ''

stroka = ''

replace_val = [('\\', '_'),('/', '_'),(':', '_'),('*', '_'),('?', '_'),('"', '_'),('<', '_'), ('>', '_'), ('|', '_')]

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

def SaveList(fn, L, cco):

, ,if cco > 0:

, , , ,f = open(fn, 'w')

, , , ,for i in L:

, , , , , ,f.write(i)

, , , ,f.close()

def SaveError(fn, L):

, ,if len(L) > 0:

, , , ,f = open(fn, 'w')

, , , ,for i in L:

, , , , , ,f.write(str(i)+'\n')

, , , ,f.close()

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

#**********************************************************************

def parse_fb2(fn):

, ,global Books

, ,global stroka

, ,global book_title

, , , ,

, ,if isinstance(fn, str):

, , , ,fn = open(fn)

, ,try:

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

, , , ,

, ,except:

, , , ,print('Error in fb2:')

, , , ,Errror.append(fn)

, , , ,return False

, ,else:

, , , ,dom.normalize()

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

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

##

, , , ,book=titl.getElementsByTagName("book-title")[0]

, , , ,book_title = book.childNodes[0].nodeValue

, , , ,book_title = book_title.replace('|', '!')

, , , ,s = book_title +'|'

, , , ,

, , , ,au = ''

, , , ,try:

, , , , , ,for auto in titl.getElementsByTagName("author"):

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

, , , , , , , ,au = au + no1.childNodes[0].nodeValue

, , , , , , , ,no1=auto.getElementsByTagName("first-name")[0]

, , , , , , , ,fi = no1.childNodes[0].nodeValue

, , , , , , , ,au = au + '.'+ fi[0]+'^' , , , , , ,

, , , , , ,au = au[:-1]

, , , ,except:

, , , , , ,au = "NoName."

, , , ,s = s+au

, , , ,stroka = s +'|0|'

, , , ,return True

#**********************************************************************

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

def parse_zip(adr):

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

, ,filelist = z.namelist()

, ,filelist.sort()

, ,for n in filelist:

, , , , try:

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

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

, , , , except:

, , , , , ,print( "Errror:", n )

, , , , , ,return False

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

def replace(line, old_new_num):

, ,for vals in old_new_num:

, , , , , ,# распаковываем кортеж

, , , , , ,old, new = vals

, , , , , ,line = line.replace(old, new)

, ,return line

def my_rename(adr,ph, ex):

, ,co = 0

, ,book_ti = book_title[:20]

# \ / : * ? " < > |

, ,book_ti = replace(book_ti, replace_val)

, ,

, ,a = '-' + book_ti + ex

, ,while os.path.isfile(os.path.join(ph, a)):

, , , ,co += 1

, , , ,a = '-' + book_ti +str(co)+ ex

# , ,print(a)

, ,os.rename(adr,os.path.join(ph, a))

, ,if ex == '.fb2':

, , , ,ex = 'f'

, ,else:

, , , ,ex = 'z'

, ,if co > 0:

, , , ,ex = str(co)+ex

, ,

, ,Books.append(stroka +ex+ '|'+str(siz)+'\n')

, ,

def parse_file(adr,ph):

, ,global siz # размер файла

, ,global countFiles

, ,flag = os.path.basename(adr)[0] != '-'

, ,siz = os.path.getsize(adr)

, ,m = adr.split(".")

, ,if (m[-1] == "zip") and (m[-2] == "fb2"):

, , , ,if flag and parse_zip(adr):

, , , , , , countFiles += 1

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

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

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

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