Write a recent learning point of Knowledge
1. File read/write
open a file:
F=open (' path ', ' R ', encoding= ' utf-8 ')
file to read ' R ' opens, can read the contents of the file, but cannot write
F=open (' Path ', ' W ', encoding= ' utf-8 ')
file to write ' W ' way open, can write the contents of the file, but cannot read out the contents of the file
open the file as read, call the Read () method can read the entire contents of the file at one time, Python content
read into memory, Use a str object to represent
open files in a written manner, and call the Write () method to write the contents into a file
finally call the Close () method to close the file. The file must be closed because the file object occupies the operating system's
resources, and the number of files that the operating system can open at the same time is limited
Python introduces the With statement to automatically help us call the Close () method:
with Open (' path ', ' R ') as F:
print (F.read ())
text file to be written to a specific encoding, To pass the encoding parameter to the open () function, the string is automatically converted to the
specified encoding.
Note:
1. When reading a file, the first time you read the cursor will move to the end, want to continue to read again to move the cursor back to the beginning of
2. Call the Tell () function to get the position of the cursor, Call the Seek () function to move the cursor to the specified position
3. To append ' a ' to open the file, both readable and writable
4. To read one line at a time, call the ReadLine () function
5. Call the ReadLines () function to read the contents of the file in a branch , and save as a list, each row as an
element of the list.
2. use of circular statements
Circular statements are commonly used while and for two forms, drawing flowchart helps us to clear up ideas, better practice using
Cycle
For x in ... The loop is to iterate over each element in the list or tuple, substituting the variable, and then performing the indentation
The statement of the block
For x in range () to generate an integer sequence
Python Learning notes (i)