forked from wangandi520/andyspythonscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
每行首行尾添加html标记p.py
50 lines (43 loc) · 1.36 KB
/
每行首行尾添加html标记p.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# encoding:utf-8
# Programmed by Andy
from pathlib import Path
import sys
import time
def readfile(filename):
# readfile
with open(filename, mode='r', encoding='UTF-8') as file:
filereadlines = file.readlines()
return filereadlines
def writefile(filename,filereadlines):
# write file
newfile = open(Path(filename).stem + '.txt', mode='w', encoding='UTF-8')
newfile.writelines(filereadlines)
newfile.close()
def convertHTML(filename):
# readfile
filereadlines = readfile(filename)
# bookname,author style
eachcontent = []
for eachLine in filereadlines:
eachLine2 = eachLine.strip().replace('\n', '').replace('\t', '').replace('\r', '').strip()
eachLine3 = '<p>' + eachLine2 + '</p>\n'
if eachLine3 == '<p></p>\n':
eachLine3 = '\n'
eachcontent.append(eachLine3)
# write file
writefile(filename,eachcontent)
def main(inputPath):
del inputPath[0]
for aPath in inputPath:
if Path.is_dir(Path(aPath)):
for file in Path(aPath).glob('*.txt'):
convertHTML(file)
if Path.is_file(Path(aPath)):
if (Path(aPath).suffix.lower() == '.txt'):
convertHTML(aPath)
if __name__ == '__main__':
try:
if len(sys.argv) >= 2:
main(sys.argv)
except IndexError:
pass