任务需求:
描述:客户给定目录路径,和一个txt文件,txt文件中放的是task的名字,根据txt的名字去搜索对应指定目录下文件(这里的是log文件),客户需要一个exe文件,双击就能看到文件的信息。
1.有个用户输入,然后去判断文件是否存在,不存在返回相应的出错信息,存在则进行下一步。(这里txt名也许是www,但是让你找的是reptsak_www.log文件,需要在代码修改格式)
2.进入文件,查看里面的日期和当前日期作对比,不一样则返回错误,一样则进行下一步。
3.找到文件的中的关键字“all tables are completed”,有的话则返回" tsak is ok"没有的话则返回“task is not ok”
解决方法:这里的任务就是写个关于文件处理的脚步,首先会想到用shell,但是在win7下面,要安装shell的模拟环境,客户是不会去这样做的,此方法放弃。
于是想到用Python写,在将Python文件(.py)文件编译成exe文件。用的工具是Pyinstaller,这是一个非常好的工具。具体实现方法可以去看链接:
http://blog.csdn.net/daniel_ustc/article/details/15501385 写的很详细,有下载链接(自己下载过可以用)
http://m.blog.csdn.net/blog/DianaCody/39762135 写的具体
这个过程中出现一些问题:
1.按照别人给的方法,始终不能生产dist目录下的exe文件,原因pyinstaller用具没有安装好(官网始终是好的),去官网下载结果可以了。
2.py文件生成的exe,单击总是运行后闪退,根本来不及看清运行的结果是什么。根据第二个链接中给的再代码最后加raw_input(),结果就好了,如果还不可以,你自己试试双击py文件也是不可以的,这样的话应该是代码的中文编码问题了,我代码不多,于是把代码里的中文都换成 了英文,结果就好了,不改变中文的方法还没有想到。如果你有好的方法的话请给我留言。
好了,工具完成了。下面就是代码的事,我就直接放上去了,有兴趣的可以自己下载下来看看。
import os
import time
import re
arr=[]
lj=[]
i=0
pre='reptask_'
path=raw_input("please input your log path:") #log path
with open('taskname.txt','r') as f:
for lines in f:
lines=lines.replace("\n","").split(",")
arr.append(lines)
for i in range(len(arr)):
arr[i].append('.log')
em=''.join(arr[i])
df=pre+em
ltime = time.strftime('%Y-%m-%d',time.localtime(time.time()))
lj = [path,df]
a = "\\"
b = a.join(lj)
print b
c = os.path.exists(b)
#print c
if c:
wf = open(b,"r+")
content = wf.read()
li = re.findall("All tables are loaded",content)
if len(li)>0:
print "%s is comleted" %arr[i][0]
else:
print "%s is not complete" %arr[i][0]
qtime = content[10:20]
print "task completion time :%s"%qtime
print "loacaltime:%s"%ltime
if qtime == ltime:
print "the time is same"
else:
print "the time is not same"
wf.close()
else:
print 'task is not exist,please check your taskname file(txt)'
raw_input()
代码.rar
描述:客户给定目录路径,和一个txt文件,txt文件中放的是task的名字,根据txt的名字去搜索对应指定目录下文件(这里的是log文件),客户需要一个exe文件,双击就能看到文件的信息。
1.有个用户输入,然后去判断文件是否存在,不存在返回相应的出错信息,存在则进行下一步。(这里txt名也许是www,但是让你找的是reptsak_www.log文件,需要在代码修改格式)
2.进入文件,查看里面的日期和当前日期作对比,不一样则返回错误,一样则进行下一步。
3.找到文件的中的关键字“all tables are completed”,有的话则返回" tsak is ok"没有的话则返回“task is not ok”
解决方法:这里的任务就是写个关于文件处理的脚步,首先会想到用shell,但是在win7下面,要安装shell的模拟环境,客户是不会去这样做的,此方法放弃。
于是想到用Python写,在将Python文件(.py)文件编译成exe文件。用的工具是Pyinstaller,这是一个非常好的工具。具体实现方法可以去看链接:
http://blog.csdn.net/daniel_ustc/article/details/15501385 写的很详细,有下载链接(自己下载过可以用)
http://m.blog.csdn.net/blog/DianaCody/39762135 写的具体
这个过程中出现一些问题:
1.按照别人给的方法,始终不能生产dist目录下的exe文件,原因pyinstaller用具没有安装好(官网始终是好的),去官网下载结果可以了。
2.py文件生成的exe,单击总是运行后闪退,根本来不及看清运行的结果是什么。根据第二个链接中给的再代码最后加raw_input(),结果就好了,如果还不可以,你自己试试双击py文件也是不可以的,这样的话应该是代码的中文编码问题了,我代码不多,于是把代码里的中文都换成 了英文,结果就好了,不改变中文的方法还没有想到。如果你有好的方法的话请给我留言。
好了,工具完成了。下面就是代码的事,我就直接放上去了,有兴趣的可以自己下载下来看看。
import os
import time
import re
arr=[]
lj=[]
i=0
pre='reptask_'
path=raw_input("please input your log path:") #log path
with open('taskname.txt','r') as f:
for lines in f:
lines=lines.replace("\n","").split(",")
arr.append(lines)
for i in range(len(arr)):
arr[i].append('.log')
em=''.join(arr[i])
df=pre+em
ltime = time.strftime('%Y-%m-%d',time.localtime(time.time()))
lj = [path,df]
a = "\\"
b = a.join(lj)
print b
c = os.path.exists(b)
#print c
if c:
wf = open(b,"r+")
content = wf.read()
li = re.findall("All tables are loaded",content)
if len(li)>0:
print "%s is comleted" %arr[i][0]
else:
print "%s is not complete" %arr[i][0]
qtime = content[10:20]
print "task completion time :%s"%qtime
print "loacaltime:%s"%ltime
if qtime == ltime:
print "the time is same"
else:
print "the time is not same"
wf.close()
else:
print 'task is not exist,please check your taskname file(txt)'
raw_input()
![fj.png](/image/default/fj.png)
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29716561/viewspace-1695296/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29716561/viewspace-1695296/