One, the file processing read operation
1.
f=Open' Chen granule ',encoding=' Utf-8 ')
Data=f.read ()
Print (data)
F.close ()
2. Open mode
R W A
3. Encoding encoding
f=Open' Chen granule ',' R ',encoding=' Utf-8 ')
Data=f.read ()
# Print (data)
4. Read a row of ReadLine
Print (F.readable ()) determines whether it is readable, returns a Boolean value
Print (' line 1th ', F.readline (),end=')
Print (' line 2nd ', F.readline ())
5. Read multiple lines ReadLines
Data=f.readlines ()
Print (data)
F.close ()
Second, the file processing write operation
File exists, after clearing the contents of the file, after the next operation
File does not exist, it can create a new file
f=Open' Chen granule 1 ',' W ',encoding=' UTF8 ')
1. Write directly
F.write (' 11111111\ n‘)
F.write (' 333\ n4444\ n555\n ")
2. Writable
F.writable ()
3. Write readlines through list
F.writelines ([ "555\n ," 6666\n '])
F.writelines ([ ' 555\n ", 6666\n ' ,1") Span style= "COLOR: #808080" The contents of the ># file can only be strings, only the string
F.close ()
Third, file processing append
f=Open (' Chen granule 1 ',' A ',encoding=' Utf-8 ')
F.write (' La la la la cheer')
Iv. other modes of file handling
1. File modification
src_f=Open' xxx ',' R ',encoding=' GBK ')
dst_f=Open' xxx ',' W ',encoding=' GBK ')
with open ( ' xxx ' , ' R ' , encoding= ' GBK ') as src_f< Span style= "COLOR: #cc7832" >,\
open ( ' xxx_new ' , ' W ' , encoding= ' GBK ') as dst_f:
Data=src_f.read ()
Dst_f.write (data)
F=open ( ' a.txt ')
print (f.encoding) #查看文件编码
2.tell
3.seek 012
4.flush
#讲文件内容从内存刷到硬盘
#文件如果关闭则返回True
#查看使用open打开文件的编码
#查看文件处理当前的光标位置
F.seek (#从开头开始算, move the cursor to the third byte
F.truncate (#从开头开始算, keep files only from 0-10 bytes of content, files must be opened in writing, except W and w+
< Span style= "COLOR: #cc7832" > &NBSP;
< Span style= "COLOR: #6a8759" > < Span style= "COLOR: #6a8759" > < Span style= "COLOR: #6a8759" > < Span style= "COLOR: #808080" >&NBSP;
Python Learning day-7 file processing