实验室项目的工作站每天会产生数量巨大的用户日志,所以每次要拷取打包后的可执行文件总是发现文件好几个G,所以需要一个自动可执行脚本对此进行处理,为此我用python写了一个自动处理的脚本。当然这个脚本比较简单只有删除 特定文件以及特定目录下的某些文件和某些目录。现将其与大家分享一下。
#!/usr/bin/python
#########################################
# file or directory processing #
# 2013-11-4 authorized by zx #
#########################################
import time
import shutil
import os
#AbPath =["/1/1.c","/1/2.c","F:/2","../1"]
AbPath = ["/1"]
print "---------------------------------------------"
print "start time:",
print time.strftime('%Y.%m.%d-%H.%M.%S',time.localtime(time.time()))
NumOfPath = 0
while NumOfPath<len(AbPath):
if os.path.exists( AbPath[NumOfPath] ):
if os.path.isdir(AbPath[NumOfPath]):
print AbPath[NumOfPath],
print " dir exist..."
shutil.rmtree(AbPath[NumOfPath])
print " rm OK"
NumOfPath = NumOfPath + 1
#elif os.path.isfile(Path[NumOfPath]: #not right why ?
else:
print AbPath[NumOfPath],
print " file exist..."
os.remove(AbPath[NumOfPath])
print " rm Ok"
NumOfPath = NumOfPath + 1
else:
print AbPath[NumOfPath],
print " file or dir not exist ,exit"
NumOfPath = NumOfPath + 1
print"end time:",
print time.strftime('%Y.%m.%d-%H.%M.%S',time.localtime(time.time()))
end = time.time()
#print end-begin
print "--------------------------------"
#os.system('pause') #not right why
raw_input("Press any key to exit!")
当然上面的程序不是实验室所用的最终版不过总体框架差不多,也就是python的文件处理。该程序相当简单在此就不多说了,不过linux下的字符编码格式和window台不一样,当时在linux下写好在windows平台下运行时发现不识别汉字。最终的解决办法是设置其格式为utf8,具体设置大家可以百度下。这个程序写的时间长了忘记了,不过就是在前面加一句话很简单
。