Python will be saved as a txt file data approach

Open = F ( ' name.txt ' , MODE = ' W ' )      # open a file, the file is automatically created when the system does not exist. 
                                 # Parameter name filename, mode mode. 
                    # W r can only read operation of writing to a file addition 
                    # W + r + readable and writable read-write read and append a + 
                    # WB + writing binary data 
                    # W mode opens the file, if the file data is written as again will overwrite the original      

f.write ( ' Hello Word Hello \ n- ' )   # write write 

f.writelines ([ ' Hello \ n- ' , ' World \ n- ' , ' Hello \ n' , ' World \ the n- ' ]) # writelines () function will list a string to the file, but does not wrap, wrap, if desired, add manual line breaks 
                                                    # parameters must be stored only in a string list 
f.close ()               # close the file                             

 

 

# Use r mode opens the file, do read the file operation 
# open the file mode, the default mode is r, if only to read the file, you can not fill mode mode 
f = Open ( ' name.txt ' , mode = ' r ' ) 

# read (int) function to read the file contents. If you specify a read length, will follow to read length, all of the data is not specified the default read 
Content = reached, f.read (10 )
 Print (Content) 

# the readline (int) function reads the default file a line of data 
content = f.readline ( 10 )
 Print (Content) 

# data the readlines () in each line will return to the list as an element, all the read data lines 
Contents = f.readlines ()
 Print (Contents) 
f.close

 

Guess you like

Origin www.cnblogs.com/ngxt/p/11919397.html