-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_tools.py
78 lines (57 loc) · 1.7 KB
/
common_tools.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
def setReadOnly(fileName):
os.system('attrib +r \"'+fileName+'\"')
def getDefaultLogger(fileName, lvl=None):
import logging
logObj = logging.getLogger()
if not lvl: lvl = logging.DEBUG
logging.basicConfig(filename=fileName, level=lvl, \
format='%(levelname)s:%(message)s')
return logObj
def renameWithBase(name, length=3):
import os
import glob
tmp = name.split('.')
if len(tmp) == 1:
prefix = name
postfix = ''
elif len(tmp) == 2:
prefix = tmp[0]
postfix = '.'+tmp[1]
else: raise Exception("wrong format", tmp)
stdLength = len(name)+length
form = prefix+'?'*length + postfix
alls = glob.glob(form)
if len(alls) == 0:
middle = '0'*length
else:
middle = int(alls[-1][len(prefix):len(prefix)+length]) + 1
if len(str(middle)) > length:
raise Exception("out of range.", middle, length)
form = '{:0'+str(length)+'d}'
middle = form.format(middle)
return prefix + middle + postfix
def writeBinary(f, para, content):
from struct import pack
f.write(pack(para, content))
def writeInt(f, content):
writeBinary(f, 'i', content)
def writeFloat(f, content):
writeBinary(f, 'f', content)
def readBinary(f, para):
from struct import unpack
return unpack(para, f.read(4))
def readInt(f):
return readBinary(f, 'i')[0]
def readFloat(f):
return readBinary(f, 'f')[0]
if __name__ == "__main__":
import os
os.chdir('testFolder')
for i in range(100):
f = open(renameWithBase("test.aa"),'w')
f.close()
os.system("pause")
import glob
for f in glob.glob("test*.aa"):
os.system("del "+f)