#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os
# преобразование файла fb2 в каталог с папками и файлами
#--------------------------------------------------
stack = []
List = []
#---
def SaveList(fn, L):
, ,f = open(fn, 'w', encoding="utf-8")
, ,for i in L:
, , , ,f.write(my_str(i))
, ,f.close()
def my_str(s):
, ,s=s.replace(' , ,',' ')
, ,s=s.replace('>','**')# вставьте вместо ** символ "больше"
, ,s=s.replace('<','**')# вставьте вместо ** символ "меньше"
, ,return s
#----------------------------------------
path = os.getcwd()
cur_dir = path
fb2_file = 'fb.fb2'
if not os.path.isfile(fb2_file):
, ,sys.exit(fb2_file + ' - does not exist')
folder = False
text_f = False
Other = False
f =open(fb2_file, encoding="utf-8")
Li=f.readlines()
for i in Li:
, ,if Other:
, , , ,if i.find('</section')==0:
, , , , , ,Other = False
, , , ,continue
, ,if text_f:
, , , ,if i.find('</section')==0:
, , , , , ,text_f = False
, , , , , ,SaveList(file_name, List)
, , , ,elif i.find('<title>')==0:
, , , , , ,s = i[10:-13]
, , , , , ,file_name = os.path.join(cur_dir, s)
, , , ,elif i.find('</p>')>-1:
, , , , , ,i = ''
, , , ,else:
, , , , , ,List.append(i[3:]) , ,
, , , ,continue
, , , ,
, ,if folder:
, , , ,if i.find('<title>')==0:
, , , , , ,s = i[10:-13]
, , , , , ,cur_dir = os.path.join(cur_dir, s)
, , , , , ,stack.insert(0,cur_dir)
, , , , , ,if os.path.isdir(cur_dir):
, , , , , , , ,sys.exit(cur_dir + ' - exists')
, , , , , ,os.mkdir(cur_dir)
, , , , , ,folder = False
, , , ,continue
, ,if i.find('<section')==0:
, , , ,if i.find('type="d"')> -1:
, , , , , ,folder = True
, , , ,else:
, , , , , ,if i.find('type="f"')> -1:
, , , , , , , ,text_f = True
, , , , , , , ,List.clear()
, ,if i.find('</section')==0:
, , , ,if len(stack)>0:
, , , , , ,stack.pop(0)
, , , , , ,if len(stack)>0:
, , , , , , , ,cur_dir = stack[0]
print( 'Done!')
Конец скрипта fb2_dir.py