Getting started with python,

Source: Internet
Author: User

Getting started with python,

Based on the file operations in the interactive window (the file name is user_list.cfg ):

>>> Print (open ('user _ list. cfg ', 'R'). read ())
User1: password1
User2: password2

>>> Print (open ('user _ list. cfg ', 'R '). readlines () [0]. split (':') [0]. strip ())
User1
>>> Print (open ('user _ list. cfg ', 'R '). readlines () [0]. split (':') [1]. strip ())
Password1
>>> Print (open ('user _ list. cfg ', 'R '). readlines () [1]. split (':') [0]. strip ())
User2
>>> Print (open ('user _ list. cfg ', 'R '). readlines () [1]. split (':') [1]. strip ())
Password2

Where:

Readlines () indicates reading the entire file and forming a list;

Readline () indicates reading a row at a time;

Read () indicates reading the entire file;

Split () indicates that strings are separated;

Strip () indicates removing spaces and line breaks at the beginning and end of a string;

 

Readlines () method:

>>> Print (open ('user _ list. cfg ', 'R'). readlines ())
['User1: password1 \ n', 'user2: password2 \ n']

We can see that the above method can read the entire file to form a list, including the special characters at the end of the line;

 

Read () method:

>>> Print (open ('user _ list. cfg ', 'R'). read () [1])
S
>>> Print (open ('user _ list. cfg ', 'R'). read () [0])
U
>>> Print (open ('user _ list. cfg ', 'R'). read () [2])
E
>>> Print (open ('user _ list. cfg ', 'R'). read () [3])
R
>>> Print (open ('user _ list. cfg ', 'R'). read () [4])
1

As shown in the preceding figure, the corresponding characters are extracted;

 

Readline () method:

>>> Print (open ('user _ list. cfg ', 'R'). readline (5 ))
User1

As shown above, the first five characters can be taken out;

 

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.