python 【练习1】资产信息扫描
#!/usr/bin/env python# -*- coding: utf-8 -*-
# @Time : 2017/11/21 0021 16:01
# @Author: ming
"""
把重复的,差集保存在文件中还没做
"""
import re
import stat, os
import time
def Number():
"""
:在第一行输出总资产数量信息
"""
filename = "Asset1.txt"
with open(filename) as myfile:
lines = len(myfile.readlines())
myfile.close()
print "\033 in %s\033[0m" % (lines, filename)
def Repeat(asset):
"""
:param asset:资产信息参数
:return: True代表资产重复,False代表资产没有重复
"""
with open("Asset1.txt") as myfile:
for i in myfile.readlines():
asset2 = re.sub("-", "\-", asset)
p = re.compile("%s$" % asset2)
existence = p.findall(i)
if len(existence) != 0:
if asset == existence:
return True
return False
def SCAN():
locate = "NONE"# 默认位置
ass = "NONE"# 默认资产
prompt = "This is prompt information"# 默认提示语
rock_list = []# 当前机架资产列表
os.chmod('Asset1.txt', stat.S_IWRITE)# 取消只读
flag = True
with open("Asset1.txt", "r+") as file:
file.seek(0, 2)
while flag:
os.system("cls")
Number()# 在第一行输出总资产数量信息
print ("\033[1;33;44mCurrent rack locate: %s\033[0m" % locate)# 输出当前所在机架
print (prompt)# 输出操作提示语
print ("The current rack has scanned asset num: %d" % len(rock_list))# 输出当前机架已经扫描的资产信息
for i in rock_list:# 打印当前机架已经扫描的资产
print i
scan = raw_input("Please scan[\"Q\" to quit]:")
scan = scan.strip().upper()
if re.search("R\d{1,2}C\d{1,2}", scan):# 判断是否为位置信息
locate = scan
prompt = "\033[1;32;44mLocation update successful\033[0m"
del rock_list[:]
continue
elif re.search("\w+-\w-\w+", scan):# 判断是否为资产信息
asset = scan
repeat = Repeat(asset)# 判断资产是否重复
if repeat == True:
prompt = "\033[1;31;44mAsset information repetition!!!\033[0m"
repeat_data = ("%s:%s" % (locate, asset))# 重复资产写入新文件
with open("Repeat.txt", "r+") as repete_file:
repete_file.seek(0, 2)
repete_file.write("%s\n" % repeat_data)
repete_file.flush()
continue
elif repeat == False:
pass
elif scan == "Q" or scan == "q":# 判断是否退出
break
else:# 输入错误
prompt = "\033[1;31;44mInput error!!!\033[0m"
continue
data = ("%s:%s" % (locate, asset))
if locate != "NONE" and asset != "NONE" and scan != "" and repeat != True:# 写入文件
file.write("%s\n" % data)
file.flush()
rock_list.append(asset)
prompt = "\033[1;35;44mAsset update successful\033[0m"
else:
prompt = "\033[1;35;44mLocation or assets are empty\033[0m"
os.chmod('Asset1.txt', stat.S_IREAD)# 设置只读
def CHECK():
"""
#比对
:return: set1中存在set2中不存在;set2中存在set1中不存在
"""
set1 = set([])
set2 = set([])
os.chmod('Asset1.txt', stat.S_IWRITE)# 取消只读
with open("Asset1.txt", "r+") as file1:
for line1 in file1.readlines():
set1.add(line1.strip())
with open("Asset2.txt", "r+") as file2:
for line2 in file2.readlines():
set2.add(line2.strip())
return set1.difference(set2), set2.difference(set1)
os.chmod('Asset1.txt', stat.S_IREAD)# 设置只读
def DUP():
list3 = []
set4 = set([])
with open("Repeat.txt", "r+") as file1:
for line1 in file1.readlines():
list3.append(line1.strip())
set4 = set(list3)
return set4
def reset():
os.chmod('Asset1.txt', stat.S_IWRITE)# 取消只读
sure = raw_input("Are you sure clear all data:")
if sure == "y" or sure == "Y" or sure == "yes" or sure == "YES":
with open("Asset1.txt", "w") as f1:
pass
with open("Asset2.txt", "w") as f2:
pass
with open("Repeat.txt", "w") as f3:
pass
os.chmod('Asset1.txt', stat.S_IREAD)# 设置只读
def main():
print "1.Scan assets"
print "2.Assets verification"
print "3.Duplicated assets"
print "4.Clear all data"
num = raw_input("please choise:")
if num == "1":
SCAN()
elif num == "2":
A, B = CHECK()
print ("IN Asset1.txt NOT IN Asset2.txt")
for i in A:
print i
print ("IN Asset2.txt NOT IN Asset1.txt")
for i in B:
print i
elif num == "3":
C = DUP()
for i in C:
print i
elif num == "4":
C = reset()
raw_input("\nEnter to Quit!:")
if __name__ == '__main__':
main()
页:
[1]