batch_b64.py
1.7 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding:utf-8 -*-
#
# 这是把当前目录所所有康虎云报表模板文件转成base64的代码
#
# 用法:
# python.exe batch_b64.py
#
#
import os
import base64
class CFTemplateToBase64():
def __init__(self):
self.path = os.path.split(os.path.realpath(__file__))[0]
def to_base64(self):
filelist = os.listdir(self.path)
total_num = len(filelist)
i = 0
for item in filelist:
if item.endswith('.fr3'):
src = os.path.join(os.path.abspath(self.path), item)
dst = os.path.join(os.path.abspath(self.path), item + '.b64')
print 'converting %s to %s ...' % (src, dst)
fin = open(src, "rb")
fout = open(dst, "w")
base64.encode(fin, fout)
fin.close()
fout.close()
i = i + 1
print 'total %d files, %d files converted base64' % (total_num, i)
def from_base64(self):
filelist = os.listdir(self.path)
total_num = len(filelist)
i = 0
for item in filelist:
if item.endswith('.b64'):
src = os.path.join(os.path.abspath(self.path), item)
dst = os.path.join(os.path.abspath(self.path), item.rstrip('.b64'))
print 'converting %s to %s ...' % (src, dst)
fin = open(src, "rb")
fout = open(dst, "w")
base64.decode(fin, fout)
fin.close()
fout.close()
i = i + 1
print 'total %d files, %d files converted from base64' % (total_num, i)
if __name__ == '__main__':
conv = CFTemplateToBase64()
conv.to_base64()