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;