7.5 File Operations

File Operations

 

what document?

  Operating system provides a simple interface to a user operation complicated hardware (hard disk) of

Why manipulate files

  People or applications need to save the data permanently

File open process

  • step1 Click to open file
  • step2 operating system receives a command, transferring the file path to the CPU
  • step3 CPU based on the path to the hard disk to find the file, and then retrieved into memory
  • step4 operating system file retrieval is displayed, you can read and write operations preservation

File should be opened

  r read-only mode

  w write-only mode

  a write append mode

File operations unit way

  t t need to specify the text file encoding parameter when in use if you do not know the default operating system's default encoding
  b binary (as it has been developed in binary mode) so be sure not specify the encoding parameters

mode parameter

  Can not write do not write the words rt default is read-only text file that default is not to write t t

  r mode

Open with (R & lt ' D: \ projects the Python \ day07 \ a.txt ' , MODE = ' R & lt ' , encoding = ' UTF-. 8 ' ) AS F:
     Print (f.readable ())   # is readable 
    Print (F .writable ())   # is writable 
    Print (f.read ())   # -time read all the contents of the file
# R & lt mode when opening the file if the file does not exist directly given 
# file path relative paths can be written but note that the file must be performed at the same layer as the file File 
with Open (R & lt ' a.txt ' , MODE = ' R & lt ' , encoding = ' UTF-. 8 ' ) AS F: 
with Open (R & lt ' a.txt ' , ' R & lt ' , encoding = ' UTF-. 8 ' ) AS F1: MODE key can not write
     Print (f.readable () )   # is readable 
    Print (f.writable ())   # is writable 
    Print ( " >>> 1: " )
     Print(reached, f.read ())   # disposable contents of the file to read all 
    Print ( ' >>> 2: ' )
     Print (reached, f.read ())   # cursor once after the file has been read in the end of the file, read not read the contents of 
    Print (f.readlines ())   # returns a list of elements in the list corresponding to a file line by line is the content 
    for line in F:   # F may be sequentially read for each loop for loop line content 
        Print (i)   # this method can solve a one-time reading large files take up too much memory problems 
    Print (f.readline ())   # only read the contents of files row 
    Print (f.readline ())
     Print (f .readline ())
     Print (f.readline ())

  w Mode (caution)

# Automatically creates a file does not exist in the case 1. This document 
# 2. When the presence of a file is written to empty the contents of the file and then 
with Open (R & lt ' xxx.txt ' , MODE = ' W ' , encoding = ' UTF -8 ' ) AS F:
     Print (f.readable ())   # is readable 
    Print (f.writable ())   # is writable 
    f.write ( ' no, no, you do not turn ~ \ n- ' ) 
    F. Write ( ' no, no, you do not turn ~ \ n- ' ) 
    f.write ( ' no, no, you do not turn ~ \ n- ' ) 
    f.write ( ' no, no, you do not turn ~ \ R & lt ' )
    f.write ( ' No, no, you do not turn ~ ' ) 
    L = [ ' no sdffs, sdfs has turned ~ \ n- ' , ' does not sdfsdf, you turn sdfsf ~ \ n- ' , ' no sfad not, you did not Total sa ~ \ n- ' ] 
    f.writelines (L) 
    # vertical equivalent 
    for I in L: 
        f.write (I)

  a mode

# 1. When the file does not exist in the case where the file is automatically created 
# 2. When the presence of a file, the file contents are not emptied, and finally move the cursor to the file 
with Open (R & lt ' yyy.txt ' , MODE = ' A ' , encoding = ' UTF-. 8 ' ) AS F:
     Print (f.readable ())   # is readable 
    Print (f.writable ())   # is writable 
    f.write ( ' I'm a little tail \ n- ' )

 

Guess you like

Origin www.cnblogs.com/PowerTips/p/11146087.html