python file read and write operations to change

to sum up:

mode open mode: r (read-only), w (write only), a (additional)
1.r: r The default read-only mode, if the content is written, it will error: io.UnsupportedOperation: not writable
(Read () to read all carried out after completion of a read operation, the end of the text cursor to the readline () reads a single line the readlines () reads a plurality of rows)
2.r +: read and write to write, read and write from scratch overwrite the contents of read after write cursor to follow the cursor away If the write noon, note that the encoding format encoding = utf-8
3.w: Reading will complain: io.UnsupportedOperation: not readable (Write () to write directly WriteLine () to write a single line the writelines () to write multiple lines, line feed position plus \ n)
4.w +: readable and writable Whether w, or w +, if the file exists directly emptied, then rewriting; if the file does not exist, a new file, and then write
5.a +: additional, not clear, readable and writable, if the file exists, write back ; If the file does not exist, create a new file, and then write
# Reads r (master key) 
File = Open ( " D: \ Test1 \\ test.txt " , ' r ' , encoding = ' UTF-8 ' )
RES = File.read () # Read () to read all carried out after completion of a read operation, the end of the text cursor to the readline () reads a single line the readlines () reads a plurality of rows 
Print (RES)

# Append a (master key) 
File = Open ( " D: \ Test1 \\ test1.txt " , ' A + ' , encoding = ' UTF-8 ' )
file.write ( " \ the n-waiting Seventeen " )

# Readable and writable + R & lt 
File Open = ( " D: \ \\ the test1.txt Test1 " , ' R & lt + ' , encoding = ' UTF-. 8 ' )
a file.write ( ' Kite Hello ' )

# 写w
file = open("D:\Test1\\test1.txt",'w',encoding='utf-8')
a file.write ( ' Hello ' )   # (Write () to write directly WriteLine () to write a single line the writelines () to write multiple lines, line feed position plus \ n)

# Readable and writable + W 
File Open = ( " D: \ \\ the test1.txt Test1 " , ' W + ' , encoding = ' UTF-. 8 ' )
a file.write ( ' club pyridazin ' )

 

Guess you like

Origin www.cnblogs.com/kite123/p/11669836.html