Выбрать главу

, , , , , ,notbinary = True

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

, ,w.writestr(NameFb2, vi.getvalue()) , ,

, ,w.close()

, ,vi.close()

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

def o_zip(fn):

, ,global NameFb2

, ,global L

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

, ,filelist = z.namelist()

, ,for n in filelist:

, , , ,try:

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

, , , , , , , ,body = z.open(n)

, , , , , , , ,NameFb2 = n

, , , , , , , ,for line in body:

, , , , , , , , , ,L.append(str(line, 'UTF-8'))

, , , , , , , ,return True

, , , ,except:

, , , , , ,print( "error:", n )

, , , , , ,return False

#-------------------- main ------------------------

print('Удаление секций binary из файла xxxx.fb2.zip')

FileName = input('Введите имя файла:')

if FileName == '':

, , exit()

e = input('Удалить обложку? [y/n]')

if e == 'y':

, , cover = 'not'

else:

, , cover = 'cover.'

if os.path.isfile(FileName):

, ,if o_zip(FileName):

, , , ,Del_Image()

, , , ,os.remove(FileName)

, , , ,os.rename('^temp.zip', FileName)

, , , ,print("Done.")

, ,else:

, , , ,print("???")

else:

, ,print("File doesn't exists!")

pic_base64.py

# подготовка рисунка для fb2

import base64

# Автор: Abhishek Amin , ,Дата записи 09.04.2021

# + w_cat 05.07.22

fn = 'ce55.jpg'

with open(fn, 'rb') as binary_file:

, ,binary_file_data = binary_file.read()

, ,base64_encoded_data = base64.b64encode(binary_file_data)

, ,base64_message = base64_encoded_data.decode('utf-8')

, ,ff = fn + '.txt'

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

, ,f.write('<binary id="'+fn+'" content-type="image/jpg">'+'\n')

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

, ,f.write('</binary>'+'\n\n')

, ,f.close()

# осталось сделать:

# 1. определение типа файла

# , ,и изменение content-type в соответствии с типом.

# 2. упаковку группы файлов.

10

del_empty_dir.py

#!/bin/env python

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

# Удаление пустых папок

import sys, os

def del_empty_dirs(path):

, ,global co

, ,for d in os.listdir(path):

, , , ,a = os.path.join(path, d)

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

, , , , , ,del_empty_dirs(a)

, , , , , ,if not os.listdir(a):

, , , , , , , ,co += 1

, , , , , , , ,os.rmdir(a) , , , , , ,

# ------------------------------ , , , , , ,

Mpath = os.getcwd()

co = 0

del_empty_dirs(Mpath)

print('Удалено '+str(co)+' пустых папок.')

Mer.py

#!/usr/bin/env python

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

# поиск одинаковых файлов

#

import sys, os

import hashlib #

ex = 'zip'#'fb2'#'txt'#'mp4'#'gif'#'jpg'

GFiles = []

co = 0

LOut = []

def SaveInList(L3):

, ,if len(L3) > 0:

, , , ,global co

, , , ,global LOut

, , , ,for i in L3:

, , , , , ,LOut.append(i+'\n')

, , , , , ,co += 1

, , , ,LOut.append('-----\n\n')

def ScanMd5(L2):

, ,if len(L2) > 0:

, , , ,L3 = []

, , , ,L2.sort()

, , , ,flag = False

, , , ,oldMd5 = ''

, , , ,OldFile = ''

, , , ,for i in L2:

, , , , , ,m = i.split('%')

, , , , , ,ZMd5= m[0]

, , , , , ,if (not flag) and (ZMd5 == oldMd5):

, , , , , , , ,flag = True

, , , , , , , ,L3.append(OldFile)

, , , , , ,if flag:

, , , , , , , ,if ZMd5 == oldMd5:

, , , , , , , , , ,L3.append(m[1])

, , , , , , , ,else:

, , , , , , , , , ,flag = False

, , , , , , , , , ,SaveInList(L3)

, , , , , , , , , ,L3.clear()

, , , , , , , , , ,

, , , , , ,oldMd5 = ZMd5

, , , , , ,OldFile = m[1]

, , , ,SaveInList(L3) #

, ,

def ScanSize(GFiles):

, ,if len(GFiles) > 0:

, , , ,L = []

, , , ,flag = False

, , , ,oldSize = ''

, , , ,OldFile = ''

, , , ,for i in GFiles:

, , , , , ,m = i.split('%')

, , , , , ,Zaize = m[0]

, , , , , ,if (not flag) and (Zaize == oldSize):

, , , , , , , ,flag = True

, , , , , , , ,L.append(getmd5(OldFile)+'%'+ OldFile)

, , , , , ,if flag:

, , , , , , , ,if Zaize == oldSize:

, , , , , , , , , ,L.append(getmd5(m[1])+'%'+ m[1])

, , , , , , , ,else:

, , , , , , , , , ,flag = False

, , , , , , , , , ,ScanMd5(L)

, , , , , , , , , ,L.clear()

, , , , , , , , , , , ,

, , , , , ,oldSize = Zaize

, , , , , ,OldFile = m[1]

, , , ,ScanMd5(L) #

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

def getmd5(file_name):

## try

with open(file_name, "rb" ) as file_to_check:

data = file_to_check.read()

return hashlib.md5(data).hexdigest()

def parse_file(fn):

, ,global GFiles

, ,s=str(os.path.getsize(fn))+'%'#

, ,GFiles.append(s+fn)

, , , ,

def parse_dir(sSrc):

, ,global f_list

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

, , , ,# full pathname

, , , ,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':