#str2file
import os
def checkrepeat(srclist,checkstr):
if not srclist:
return -1
if len(checkstr) == 0:
return -1
for i in srclist:
i = i.strip()
checkstr = checkstr.strip()
if checkstr == i:
return 1
return 0
def str2file(strlist,input_file):
if not os.path.isfile(input_file):
print "The input file is not exist,Please check it!"
return -1
if not strlist:
print "You Can not create the new logid,Please check it!"
return -1
file = open(input_file,"r")
contentlist = file.readlines()
file.close()
flag1 = False
flag2 = False
count = 0
for i in contentlist:
pos = i.find("TLOG_FFFF")
if pos >= 0:
for j in strlist:
#print "j = ",j
ret = checkrepeat(contentlist,j)
#print "############ret = ",ret
if ret == 1:
count += 1
continue;
if j != str[len(strlist)-1]:
contentlist.insert(contentlist.index(i),j)
flag1 = True
else:
contentlist.insert(contentlist.index(i),j+'\n')
flag2 = True
if flag1 or flag2:
break
content = ''.join(contentlist)
file = open(input_file,"w")
file.write(content)
file.close()
if flag1 or flag2:
print "Add the logid successfully!"
else:
print "Add the logid faild,Because of it have " +str(count)+" repeat logids,Please check it!"
return 0
if __name__ == "__main__":
file = open("D:\Python_Learning\code.txt","r")
logstr = file.readlines()
logfilename = r"D:\Python_Learning\logid.h"
if not os.path.isfile(logfilename):
print ("The logid.h is not exist")
ret = str2file(logstr,logfilename)
if ret != 0:
print ("Add logdid faild ")
import os
def checkrepeat(srclist,checkstr):
if not srclist:
return -1
if len(checkstr) == 0:
return -1
for i in srclist:
i = i.strip()
checkstr = checkstr.strip()
if checkstr == i:
return 1
return 0
def str2file(strlist,input_file):
if not os.path.isfile(input_file):
print "The input file is not exist,Please check it!"
return -1
if not strlist:
print "You Can not create the new logid,Please check it!"
return -1
file = open(input_file,"r")
contentlist = file.readlines()
file.close()
flag1 = False
flag2 = False
count = 0
for i in contentlist:
pos = i.find("TLOG_FFFF")
if pos >= 0:
for j in strlist:
#print "j = ",j
ret = checkrepeat(contentlist,j)
#print "############ret = ",ret
if ret == 1:
count += 1
continue;
if j != str[len(strlist)-1]:
contentlist.insert(contentlist.index(i),j)
flag1 = True
else:
contentlist.insert(contentlist.index(i),j+'\n')
flag2 = True
if flag1 or flag2:
break
content = ''.join(contentlist)
file = open(input_file,"w")
file.write(content)
file.close()
if flag1 or flag2:
print "Add the logid successfully!"
else:
print "Add the logid faild,Because of it have " +str(count)+" repeat logids,Please check it!"
return 0
if __name__ == "__main__":
file = open("D:\Python_Learning\code.txt","r")
logstr = file.readlines()
logfilename = r"D:\Python_Learning\logid.h"
if not os.path.isfile(logfilename):
print ("The logid.h is not exist")
ret = str2file(logstr,logfilename)
if ret != 0:
print ("Add logdid faild ")