python 文件存取

1、创建targets文件

vi targets

$cat targets
RPC-DCOM 10.10.20.1.10.10.20.4
SQL-SA-blank-pw 10.10.20.27.10.10.20.28

2、python 

>>> targets_file = open('targets','r') //以只读方式打开文件

>>> lines = targets_file.readlines()
>>> lines
['RPC-DCOM \t\t10.10.20.1.10.10.20.4\n', 'SQL-SA-blank-pw\t\t10.10.20.27.10.10.20.28\n']

>>> lines_dictionary = {}//定义字典

>>> for line in lines:   //循环将值写入字典中
... one_line = line.split()
... line_key = one_line[0]
... line_value = one_line[1]
... lines_dictionary[line_key] = line_value

>>> lines_dictionary
{'RPC-DCOM': '10.10.20.1.10.10.20.4', 'SQL-SA-blank-pw': '10.10.20.27.10.10.20.28'}

>>> for key in lines_dictionary.keys()://分别提取键和值
... targets_string = lines_dictionary[key]
... targets_list = targets_string.split(',')
... targets_number = len(targets_list)
... filename = key + '_' + str(targets_number) + '_targets'
... vuln_file = open(filename,'w')
... for vuln_target in targets_list:
... vuln_file.write(vuln_target + '\n')
... vuln_file.close()

生成两个文件RPC-DCOM_1_targets和SQL-SA-blank-pw_1_targets

xfqdeMacBook-Pro:Desktop admin_zoe$ ls
$RECYCLE.BIN
RPC-DCOM_1_targets
SQL-SA-blank-pw_1_targets

xfqdeMacBook-Pro:Desktop admin_zoe$ cat RPC-DCOM_1_targets
10.10.20.1.10.10.20.4

xfqdeMacBook-Pro:Desktop admin_zoe$ cat SQL-SA-blank-pw_1_targets
10.10.20.27.10.10.20.28

posted @ 2014-11-28 09:41  echo1006  阅读(323)  评论(0编辑  收藏  举报