Python file access

Source: Internet
Author: User

1. Create a targets file

VI Targets

$cat targets
Rpc-dcom 10.10.20.1.10.10.20.4
sql-sa-blank-pw10.10.20.27.10.10.20.28

2. Python

>>> targets_file = open (' Targets ', ' r ')//opening file as read-only

>>> 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 = {}//definition dictionary

>>> in lines://Loop writes values to the dictionary
... 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 ()://extract keys and values separately
... 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 ()

Two files generated rpc-dcom_1_targets and 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

Python file access

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.