python file content processing (a)

Summary: Be sure to understand the rules of the cursor

-------------------------------------------------- ------------------------- first portion substantially operating --------------------- -----------------------------------------

1. The object can call the method, the object is a file, the corresponding method can be invoked. Operation file object, first create a file on the image.

2. Open the file object creation method or methods

Method a: Function open ( 'filename'), only one mandatory parameter, a file name, other parameters: open mode, encoded with more, require close () function closes. Returns the file objects (handles), in the file processing is required to use this function, if the file can not be opened, will throw OSError.

Method two: with open () ---- automatically shut down, automatically given off does not require close () function closes

3. Open mode determines what actions can be carried out, which is the document object model.

 

Basic file open mode: (1) read only R & lt; not write (2) W write only; not read (3) adding a; readable, then no new, there is added (4) a combination mode; write

First part: reading the file 'r': related method  read (); readline (); readlines ()

     Read (), taken in accordance with the character data, note the position of the cursor, the specified character length can be read: for example, read (6) read the six characters in length

     the readline (), the contents of the line fetch, including, newline will print, but also can print length parameter limits; continuous printing print, the end of the print line cursor, printing will not be repeated

     the readlines (), taking the entire contents of each line as a list of elements, a print form of a list, the for loop iterates

Part II: file write 'w': related method Write (), write to a file, note caution write mode, when opened been emptied

The third part: the additional file 'a'

Part IV: 'r +' ----- reader, to open a file for reading and writing. The file pointer will be placed at the beginning of the file.

              'W +' --- read-write, the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file

               'A +' --- read, if the file already exists, the file pointer will be placed at the end of the file. It would append mode when the file is opened. If the file does not exist, create a new file for reading and writing.

        

-------------------------------------------------- -------the third part---------------------------------------- ---------------------------------------

First, on a newline print:

Second, the position of the cursor and

seek() tell()

(1) seek () to read the file position cursor movement;

Format: file.seek (offset, whence) that is, Seek ([file offset] [file pointer position])

Description: indicates the amount of offset parameters to offset, i.e. 0 is not shifted, i.e. shifted to the right a positive number, i.e., a negative offset to the left

          Whence parameter indicates the reference position, wherein the value 0 represents whence starts from the beginning of the file, represents counted from the current position, from the end of the file represents. whence defaults to an empty value is not set to 0

(2) tell () to print the current cursor position   

Display:

Open with ( ' file1.txt ' ) CON AS:
     # Print (con.read ()) 
    Mon = con.read (. 4 )
     Print (Mon)
     Print (con.tell ())     # file object transfer 
    con.seek (1 , 0)         # from a first position to begin a character entry 
    Print (con.tell ())

 ---------- results -----
 # NO the p- 
# 4 
# 1

(3) for the same file object continuous operation is to be noted that the cursor position varies continuously

# The following writing file 
with Open ( ' runoob.txt ' , ' W ' ) AS FIB:
     Print (fib.tell ())       # source files have been formatted, the cursor is at the 0 
    fib.write ( ' 11111 ' )
     Print (fib.tell ())        # after writing at No. 5, the next one additional 
    fib.write ( ' 22222 ' ) 
    
# 1,111,122,222 file results

 Third, the cache refresh

flash () to refresh the data cache to disk, the buffer is emptied by means of an example of the progress bar to print easy to understand

 

# Achieve the progress bar 
# principle: before the file is not closed, the data is in the cache 
Import   SYS, Time 

FO = Open ( " runoob.txt " , " wb " )
 for i in the Range (30 ): 
    sys.stdout.write ( ' * ' )    # still a memory buffer 
    sys.stdout.flush ()     # flush the buffer (this line comments can observe the similarities and differences!) 
    the time.sleep (0.5 ) 
fo.close ()

 For example, this is generally not saved prevent electrical short.

 

Guess you like

Origin www.cnblogs.com/duguxiren/p/11300119.html