1. File open 1.1. File Open method
Open (Name[,mode[,buff]]->file
name--file path
mode--Open Mode
buff--Buffering buffer size
file--returns an object of type file
Important: The close () method must be written
1.2 How to open files
Other:
When opening binary files (slices), use the following method
' RB ', ' WB ', ' ab ', ' rb+ ', ' wb+ ', ' ab+ '
2. File Read2.1file Read Method
Read ([size])
reads a size byte, which reads all by default.
If size is less than or equal to the number of file bytes, The content of size is returned , and the cursor is positioned after it has been read .
ReadLine ([size])
reads a row from the current cursor.
If size is less than the number of bytes in the current row, The content of size is returned , and the cursor is positioned after it has been read .
ReadLines ([size])
reads the file once and returns a list of each row
Note:
sizeis actually useless to return to theListlength is not determined by it, but byiothe modular totalio. Default_buffer_sizedecision, and notsize=3just read3aio. Default_buffer_sizebyte-size content
is not actually a one-time read of the file, just read io. Default_buffer_size byte size (such as 8192 ) and then returns the content 8192 content of slightly larger or equal byte size (guaranteed list[-1] is a complete line).
Iter
using iterators to read files by row ( recommended method )
not affected by io. Limitations of Default_ buffer_size
>>> F=open ("helloworld.py")
>>> Iter_f=iter (f)
>>> line=0
>>> for I in Iter_f:
... line+=1
...
>>> Line
4
3. File Write3.1File Write Method
Write (str)
Writing a string to a file
Writelines (sequence_of_strings)
Provides a str or STR-composed iterator that connects each str to a file
Note: There must be a sequence of strings, otherwise an error will be made
3.2linuxWrite Cache4. File Close 4.1 Why would you like to close the file?
Write files that are cached in memory to disk
Linux, the number of files that each process can open is limited, and if this limit is exceeded, the open file will fail.
Note:
To view the limits of a process
Cat/proc/${pid}/limits
Linux process Open File limit
Http://www.cnblogs.com/aka-blog/articles/filemax.html
4.2file.fileno ()
A file descriptor that describes how many times the current file has been opened (not closed).
If the limit is exceeded, the error IO error:too many open files: ' xx '
5. File pointer 5.1seek () function
Move file pointer
Seek (offset[, whence]), None
offset--offset, can be a negative number
whence--offset relative position.
can use 0,1,2;
You can also use the macro definition of the OS module
Os. Seek_set (file start position 0)
Os. Seek_cur (file current position 1)
Os. Seek_end (file end position 2)
5.2 Examples
>>> f=open (' helloworld.py ', ' r+ ')
>>> C=f.read ()
>>> C
'helloword123\nhelloword456\nhelloword789 '
>>> F.seek (0,os. Seek_set)
>>> f.write (' helloppt ')
>>> F.seek (0,os. Seek_set)
>>> c=f.read ()
>>> C
'hellopptd123\nhelloword456\nhelloword789 '
>>> F.close ()
Python file Processing--01 File Open & Read & write & close & pointer