One, character encoding
# unicode -------> enconde( u t f - 8 ) -------> bytes
Get the bytes, you can store it in a file or transfer it over the network
# bytes --------> enconde( u t f - 8 ) -------> unicode
String encode in Python 3 gets bytes
Strings in Python 2 are bytes
Python 2 add'u' before the string, which is unicode
Process: Open the file ===> get the file handle and assign it to the variable ====> manipulate the file through the handle ==> close the file
Read the file:
1 f =open( ‘aaaa.py ’ , ‘ r ’ ,encoding = ‘utf-8’ )23print( f.readline(),end= ‘’)#Read a line
45 print( f.readlines(), end=’’ )#Read all, no line break
67 print( f.readable())#Readable file
89 print( f.writable())#Unwritable file
1011 f.close()#It must be closed after the end, otherwise it will occupy memory
Write file:
f =open( ‘new_2’, ‘w’,encoding=’utf-8’ )print( f.readable())#Unreadable
print( f.writable())#Writable
f.write( ‘888888\n’ )#Append single line
f.write( ‘999999\n’ )#Append
f.writelines([ ‘787878\n’ , ‘878787\n’])#Append multiple lines
f.close()#Close file
Append:
f=open('new_2','a',encoding='utf-8')print(f.readable()) #Unreadable
print(f.writable()) #Writable
f.write('888888\n') #Append single line
f.write('999999\n')
f.writelines(['787878\n','878787\n']) #Append multiple lines
f.close()
# rb
f=open('aaaa.py','rb') #Open in bytes
print(f.read().decode('utf-8'))
Copy picture:
f=open('1.jpg','rb') #Open 1 in rb way.JPG
data=f.read() #data gets the content of f
print(data)
f2=open('3.jpg','wb') #Open as write
f2.write(data) #Write the content of data to f2
f2.close()
f.close()
# ab, append in binary form
f=open('new_3.txt','ab')
f.write('aaaaa\n'.encode('utf-8'))
withopen('aaaa.py','r',encoding='utf-8')as read_f,\
open('aaaa_new.py','w',encoding='utf-8')as write_f:
data=read_f.read()
write_f.write(data)
Take each line of the file in a loop:
withopen('a.txt','r',encoding='utf-8')as f:while True:
line=f.readline()if not line:breakprint(line,end='')
lines=f.readlines() #Only for small files
print(lines)
data=f.read()print(type(data))for line in f: #Recommended Use
print(line,end='')
File modification:
Method 1: Only applicable to small files
1 import os
23 withopen('a.txt','r',encoding='utf-8')as read_f,\
45 open('a.txt.swap','w',encoding='utf-8')as write_f:67 data=read_f.read()89 write_f.write(data.replace('alex_SB','alex_BSB'))10111213 os.remove('a.txt')1415 os.rename('a.txt.swap','a.txt')
**# Method 2: **
import os
withopen('a.txt','r',encoding='utf-8')as read_f,\
open('a.txt.swap','w',encoding='utf-8')as write_f:for line in read_f:
write_f.write(line.replace('alex_BSB','BB_alex_SB'))
os.remove('a.txt')
os.rename('a.txt.swap','a.txt')
File function operation introduction
Function | Description |
---|---|
file.read ([size] ) | Read all contents of file |
file.readline ([size] ) | Read line by line |
file.readlines ([size] ) | Every line read as an element in list |
file.write (str ) | Write string to file |
file.writelines (sequence or stringe) | Write multiple lines to a file, the parameter can be an iterable object/list/tuple... |
file.close() | Close the opened file |
file.fileno (self ) | file descriptor |
file.flush (self ) | Flush the contents of the buffer to the hard disk |
file.isatty (self ) | Judge whether the file is a tty device, if it is a tty device, return True |
file.readable (self ) | Judge whether it is readable, return True if it is readable, otherwise return False |
file.readline (self, limit=-1 ) | Read only one line of data at a time |
file.readlines (self, hint=-1 ) | Take each line of content as an element of a list |
file.tell(self) | Get pointer position |
file.seek(self,offset, whence=io.SEEK_SET) | Specify the position of the pointer in the file |
file.seekable(self) | Is the pointer operable |
file.writable(self) | Whether it is writable |
file.writlines(self,lines) | The sequence of strings written to the file, the sequence can be any iterated object string |
file.read(self,n=None) | Read the specified byte data, and read all by default without adding parameters |
file.write(self,s) | write content to file |
Recommended Posts