1th. Document processing 1.1 file operation flow
1. Open the file, get the file handle and assign a value to a variable
2. Manipulate the file through a handle
3. Close the file
1.2 Concrete operation
1. Open the file, get the file handle and assign a value to a variable
F=open (' db.txt ',' R ', encoding=' Utf-8 ')
2. Manipulate the file through a handle
Data=f.read ()
3. Close the file
F Close () # Recycle operating system resources
1.3 Process Analysis
F=open (' db.txt ',' R ', encoding=' Utf-8 ')
1. The application initiates a system call to the operating system: open (...)
2. The operating system opens the file and returns a file handle to the application
3. The application assigns the file handle to the variable F
1.4 Resource Recycling
1. Open a file that contains two parts of the resource:
Operating system-level open files and variables for the application.
When you have completed a file, you must recycle the two parts of the file with a non-landed method:
1) F. Close () #回收操作系统级打开的文件;
2) del f #回收应用程序级的变量; the automatic garbage collection mechanism of the Python interpreter has been done for us.
Attention:
1) Del F must occur after f.close (), otherwise it will cause the operating system open files are not closed, wasting resources, remember that after the operation of the file, be sure to do f.close
2) The encoding of the open file is based on the encoding of the operating system, unless open () specifies encoding= ' encoding ')
1.5with keywords
The role of With: context management, which will help us to close the file (F.close ())
with open (' a.txt ',' R ', encoding=' utf-8 ') as F:
Data=f.read ()
# # # supports managing multiple files simultaneously
with open (' a.txt ',' R ', encoding=' utf-8 ') as Read_f,open (' B.txt ',' R ', encoding=' utf-8 ') as Write_f:
Data=read_f.read ()
Write_f.write (data)
1.6 Character encoding issues
F=open (...) is opened by the operating system file, then if we do not specify the encoding for open, then the default encoding of the opening file is obviously the operating system, the operating system will use its own default encoding to open the file:
Under Windows is GBK, which is utf-8 under Linux.
This is the use of the last lesson of the character encoding knowledge: to ensure that no garbled, the file in what way, it will be opened in what way.
F=open (' A.txt ', ' R ', encoding= ' utf-8 ')
2nd. Open File Mode
File handle = open (' File path ', ' pattern ', ' character encoding ')
2.1 Text pattern (' t ', text mode (default)
R, read-only mode "default mode, file must exist, not present, throw exception"
with open (' a.txt ',' R ', encoding=' utf-8 ') as F:
Print (F.read ())
W, write-only mode "unreadable; not exist" created; empty content "
# # # newline using ' \ n '
With open (' A.txt ', ' W ', encoding= ' utf-8 ') as F:
F.write (' Today is 2017.09.22\n ')
F.write (' Today is Friday \ n ')
Today is 2017.09.22
Today is Friday.
A, append only write mode "unreadable; not exist" create; append content only "
with open (' a.txt ',' A ', encoding=' utf-8 ') as F:
F.write (' 111\n ')
F.write (' 222\n ')
F.write (' 333\n ')
Today is 2017.09.22
Today is Friday.
111
222
333
2.2 Binary modes (' B ' binary mode)
For non-text files, we can only use B mode, "B" means to operate in bytes (and all files are stored in bytes, using this mode regardless of the text file character encoding, picture file JGP format, video file avi format)
Note: When opened in B, the content read is a byte type, and a byte type is required for writing, and encoding cannot be specified
with open (' yuanhao.jpg ', mode=' RB ') as F:
Print (F.read ())
# # # If you use B binary mode for text, remember to decode it into utf-8
with open (' a.txt ', mode=' RB ') as F:
Data=f.read ()
Print (Data.decode (' utf-8 '))
# # # written in text format, need encode Utf-8
with open (' d.txt ', mode=' WB ') as F:
F.write ( ' hahaha hello '. Encode (' utf-8 '))
2.3 Understanding Patterns
"+" means you can read and write a file at the same time
r+, read and write "readable, writable"
w+, write "readable, writable"
A +, write "readable, writable"
X, write-only mode "unreadable; not present, create, present error"
x+, write "readable, writable"
3rd. How to manipulate Files 3.1 master 3.1.1read (read)
F.read () #读取所有内容, the cursor moves to the end of the file
f.readline () # reads a line of content and moves the cursor to the beginning of the second
F.readlines () #读取每一行内容, stored in the list
# # # Read all the content, do not use read when the file is large
with open (' a.txt ',' R ', encoding=' utf-8 ') as F_read:
Print (F_read.read ())
Today is 2017.09.22
Today is Friday.
111
222
333
# # # reads content by line
with open (' a.txt ',' R ', encoding=' utf-8 ') as F_read:
Print (F_read.readline ())
Print (F_read.readline ())
Today is 2017.09.22
Today is Friday.
# # # need to add end= ' to replace the swap line \ n
with open (' a.txt ',' R ', encoding=' utf-8 ') as F_read:
Print (F_read.readline (), end=") #一次读一行
Print (F_read.readline (), end=")
Today is 2017.09.22
Today is Friday.
# # Read all, results put in the list
with open (' a.txt ',' R ', encoding=' utf-8 ') as F_read:
Print (F_read.readlines ()) #读所有, big file will be very card
[' Today is 2017.09.22\n ', ' today is Friday \ n ', ' 111\n ', ' 222\n ', ' 333\n ']
with open (' a.txt ',' R ', encoding=' utf-8 ') as F_read:
Print (F_read.readlines () [0])
Today is 2017.09.22
3.1.2write (Write)
F.write (' 1111\n222\n ') #针对文本模式的写, you need to write your own line break
F.write (' 1111\n222\n '. Encode (' Utf-8 ')) #针对b模式的写, you need to write your own line break
F.writelines ([' 333\n ', ' 444\n ']) #文件模式
F.writelines ([bytes (' 333\n ', encoding= ' utf-8 '), ' 444\n '. Encode (' Utf-8 ')]) #b模式
To write the contents of the list to a file:
l=[' 444\n ',' 555\n ',' 666\n ']
with open (' a.txt ',' A ', encoding=' utf-8 ') as F_write:
for line in L:
F_write.write (line)
Today is 2017.09.22
Today is Friday.
111
222
333
444
555
666
# # # Using the WriteLine method
with open (' a.txt ',' A ', encoding=' utf-8 ') as F_write:
F_write.writelines ([' 444\n ',' 555\n ',' 666\n '])
3.1.3
#遍历文件z
With open (' A.txt ', encoding= ' utf-8 ') as F:
#不推荐使用
# Lines=f.readlines ()
# for line in lines:
# print (line,end= ")
Recommended Use # # #
For line in F:
Print (line,end= ")
3.2 See
F.readable () #文件是否可读
F.writable () #文件是否可读
F.closed #文件是否关闭
F.encoding #如果文件打开模式为b, the attribute is not
F.flush () #立刻将文件内容从内存刷到硬盘
4th Other methods of file operation 4.1read (n)
# # # Read the file in text mode, n is the number of characters
with open (' a.txt ',' R ', encoding=' utf-8 ') as F:
Data=f.read (3)
Print (data)
Today is
# # # Read the file in B mode, n is the number of bytes
with open (' a.txt ',' RB ') as F:
Data=f.read (3)
Print (F.tell ())
Print (Data.decode (' utf-8 '))
3
This
4.2tell return cursor Position
# # Tell: tells the location of the current cursor
with open (' a.txt ',' R ', encoding=' utf-8 ') as F:
Data=f.read (3)
Print (F.tell ())
Print (data)
9
Today is
4.3seek Move Cursor
Fileobject.seek (offset[, whence])
Offset--The starting shift, which represents the number of bytes that need to be shifted
whence: optional, default value is 0. Give the offset parameter a definition of where to start the offset, and 0 to start at the beginning of the file, 1 to start at the current position, and 2 for the end of the file.
with open (' a.txt ',' R ', encoding=' utf-8 ') as F:
Data1=f.read ()
Print (' first: ', Data1)
Print (F.tell ()) #获取当前光标位置
F.seek (0) #移动到文件开头
Data2 = F.read () #由于光标移动到文件开头, so the file is completely output once
Print (' second: ', Data2)
First:abc
Efd
12
Second:abc
Efd
5th revision of the document
File data is stored on the hard disk, so there is only coverage, there is no modification so to speak, we usually see the modified files, are simulated out of the effect, specifically, there are two ways to achieve:
Mode one: The contents of the file stored in the hard disk are loaded into memory, can be modified in memory, and then overwritten by memory to the hard disk (word,vim,nodpad++ and other editors).
E TXT >>>
Alex say I has on Tesla
My name is Alex
Alex is good
Alex xxxx hahaha Alex
# # # Method One (take up too much memory, only for small files): The hard disk files to read all the data into memory, and then in memory to modify, and finally save
Import OS
with open (' e.txt ',' R ', encoding=' utf-8 ') as F_read,\
Open ('. E.txt.swap ',' W ', encoding=' utf-8 ') as F_write:
Data=f_read.read ()
Data=data.replace (' Alex ',' SB ')
F_write.write (data)
Os.remove (' e.txt ')
Os.rename ('. E.txt.swap ',' E.txt ')
# # # Way two: one line to read, one line to change
Import OS
with open (' e.txt ',' R ', encoding=' utf-8 ') as F_read, open (' E.txt.swap ',' W ', encoding=' utf-8 ') as F_write:
for n in F_read:
Data=n.replace (' SB ',' Alex ') # # # Read a line
F_write.write (data) # # # changed one line
Os.remove (' e.txt ')
Os.rename (' E.txt.swap ',' e.txt')
The 6th Chapter documents the document database
Db.txt>>>
1,peigen1,38,male,1234563378
2,peigen2,28,female,1234335678
3,peigen3,18,male,123145678
4,peigen4,8,male,1234115678
5,peigen5,48,female,1232245678
6,peigen6,58,male,1234335678
with open (' db.txt ',' R ', encoding=' utf-8 ') as F:
for line in F:
User_l=line.split (', ')
Print (User_l[1],int (user_l[2))
PEIGEN1 38
Peigen2 28
Peigen3 18
PEIGEN4 8
PEIGEN5 48
PEIGEN6 58
Fourth Python file processing