Python Learning (11) files and streams

Source: Internet
Author: User

11.1 Open File


>>> f = open (R ' C:\text\somefile.txt '), the first parameter is the file name, must have, the second is the pattern, and the third parameter is the buffer.

 11.1.1 file Mode

  If the open function takes only one filename parameter, then we can get the file object that can read the contents of the file. If you want to write content to a file, you must provide a schema parameter

' r '-----read mode (the same as the effect of non-enactment)

' W '-----write mode

' A '-----append mode

' B '-----binary mode (Python assumes that all the text files (including characters) are processed, but if it is a different type of file (binary), such as a sound or an image, specify B mode)

( why binary mode: \ n and \ r \ n are converted, but if it is a binary file, it will not go.) )

' + '-----read/write mode

11.1.2 Buffering

If it is 0 (False), I/O is unbuffered, and all read and write operations are directed against the hard disk;

If it is 1 (True), it is buffered, using memory instead of the hard disk, the speed is fast, only when flush or close to update the data on the hard disk.

Greater than 1, which represents the buffer size (in bytes), 1 represents the use of the default buffer size.

11.2 Basic File methods

 11.2.1 Read and Write

>>> f = open (R'c:\text\somefile.txt','w')      Write >>> f.write ('ABCD \ r \ n gh')15>>> F.flush ()                                  //  call Close () or flush () to actually write it in

>>> F=open (R ' C:\text\somefile.txt ', ' r ')//Read
>>> F.read (4)//Read Only 4
' ABCD '

>>> f.read ()//Read all
' \ n EF \ n gh '

11.2.2-Tube output

  The output of the previous command is the input to the next command, with the symbol "|" Connection

11.2.3 Read-write line

>>> f.readline ()  ' >>> f.seek (0)   //   Navigate to text start 0 >>>
   
     f.readline ()   //Read a line  
    '
    ABCD \ n
    ' >>>
     f.readlines ()  //Read the Some rows, return list [
    ' 
     EF \ n
    
    '
    \ n
    '
     
     gh  
    ']
    

F.writelines ([' AfA ', ' GDF ', ' GFDG ']) //write the list by line

11.2.4 Closing files

  The file operation should, in Finally, call F.close (), so that the exception can also be properly closed file.

11.2.5 using the basic file method

11.3 Iterating over the contents of a file

 11.3.1 Processing by byte 

f == f.read (1) while char:    process (char)    = f.read (1) F.close ()

11.3.2 row-by-line operation

   ReadLine ()

11.3.3 Read all content

 Read (): The Read method without parameters can read all rows.

ReadLines (): reads all rows.

11.3.4 using Fileinput to implement lazy line iterations

  ReadLine () reads one line at a time.

Import Fileimput  for inch fileinput.input (filename):    process (line)

11.3.5 File iterator

f = open (filename) for in F:    processs (line) f.close ()

Python Learning (11) files and streams

Related Article

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.