The file is the operating system for the user / An abstract unit provided by an application to operate a hard disk
user / The file read / write operation of the application program will be converted from the operating system to the specific hard disk operation
So the user / Applications can simply read \ Write files to indirectly control complex hard disk access operations
Realize the permanent saving of data in memory to the hard disk
Memory
user=input('>>>>: ') #user=" Buddy "
Basic steps of file operation :
f=open(...) # Open file , Get a file object f,f It's like a remote control , You can send instructions to the operating system
f.read() # Read and write files , Send instructions to the operating system to read and write files
f.close() # Close file , Recycle operating system resources
# Context management :
with open(...) as f:
pass
Absolute path
f=open(r'D:\python Code 6\day7\a.txt',encoding='utf-8')
f1 = f.read()
f.close()
Relative paths
f = open('./a.txt',encoding='utf-8')
f1=f.read()
print(f1)
f.close()
Current path omitted ./ The most commonly used
f = open('a.txt',encoding='utf-8')
f1=f.read()
print(f1)
f.close()
At the next higher level
f = open('../day7/a.txt',encoding='utf-8')
f1=f.read()
print(f1)
f.close()
Bold style root directory
f = open(r'\python Code 6\day7\a.txt',encoding='utf-8')
f1=f.read()
print(f1)
f.close()
Context management Commonly used writing :*
with open(...) as f:
pass
You can close files automatically Commonly used writing *
with open(r'D:\python Code 6\day7\a.txt',encoding='utf-8')as f:
print(f.read())
One File open mode
r: read only mode ( default )
w: Write only mode
a: Just add write mode
Two Controls how file units are read and written ( Must be with r\w\a Continuous use )
t : Text mode ( default ), Be sure to designate encoding Parameters
advantage : The operating system decodes the binary digits in the hard disk drive into unicode Then return
emphasize : Valid only for text files
b: Binary mode , There must be no designation of encoding Parameters
advantage : Do not specify character encoding , video , Picture file
Judge whether it is readable or writable
print(f.readable()) Is it readable? , return true/false
print(f.writable()) Is it possible to write , return true/false
mode=‘rt’ In read-only mode
1 When the file is not saved , Will report a mistake
2 When a file exists , Pointer to the beginning of the file
with open(r'D:\python Code 6\day7\a.txt', mode='rt',encoding='utf-8')as f:
f.readline() # Read only one line
f.readlines() #read The file is too large , Not good. !, Read only one line , And save it in the array
mode=‘wt’ In write only mode
1 When the file is not saved , Create a new empty document ( Nothing creates )
2 When a file exists , Empty file contents , The file pointer runs to the beginning of the file ( Empty if there is )
with open(r'D:\python Code 6\day7\a.txt', mode='wt',encoding='utf-8')as f:
f.write(' Buddy ')
f.writeline( list )
mode=‘at’ Write only mode
1 When the file is not saved , Create a new empty document , The file pointer runs to the end of the file ( The beginning is the end )
2 When a file exists , The file pointer runs to the end of the file
with open('c.txt',mode='at',encoding='utf-8')as f:
pass
Binary b Pattern Pictures and videos have nothing to do with character encoding
Open the picture file without encoding Decoding theory
read rb
with open('1.png',mode='rb') as f:
data = f.read()
print(data)
print(type(data))
Write wb
with open('2.png',mode='wb') as f:
f.write(data)
use b Pattern , You can also operate on text files , But to decode
with open('b Pattern .txt',mode='rb')as f:
data=f.read()
print(data)
# decode Binary decode to character
print(data.decode('utf-8'))
# code When writing, convert characters into binary and write
with open('wb Pattern .txt',mode='wb')as f:
f.write(' Buddy \n'.encode('utf-8'))
f.write(' Buddy \n'.encode('utf-8'))
f.write(' Buddy \n'.encode('utf-8'))
r+t
1 When the file is not saved , Will report a mistake
2 When a file exists , Pointer to the beginning of the file
3 There is an extra end to write
with open(' Can read but write r+t Pattern .txt',mode='r+t',encoding='utf-8')as f:
print(f.readable())
print(f.writable())
msg = f.readline()
print(msg)
f.write('xxxxxxxxxxxxxxx')
w+t
1 When the file is not saved , Create a new empty document ( Nothing creates )
2 When a file exists , Empty file contents , The file pointer runs to the beginning of the file ( Empty if there is )
3 You can read
with open(' Can read but write w+t Pattern .txt',mode='w+t',encoding='utf-8')as f:
print(f.readable())
print(f.writable())
f.write('aaaaaaaaaaaa\n')
f.write('bbbbbbbbbbbb\n')
# Read according to the pointer
# The pointer moves seek( Number of bytes moved , Start at the beginning 0)
# Move from the beginning 0
# I can't read it
print(f.readline())
f.seek(0,0)
print(f.readline())
# Still write at the end
f.write('ccccccccccc')
a+t
The second time I opened it, I wrote at the end
with open(' Can read but write a+t Pattern .txt',mode='a+t',encoding='utf-8')as f:
print(f.readable())
print(f.writable())
f.write('aaaaaaaaaaaa\n')
f.write('bbbbbbbbbbbb\n')
# Read according to the pointer
# The pointer moves seek( Number of bytes moved , Start at the beginning 0)
# Move from the beginning 0
# I can't read it
print(f.readline())
f.seek(0,0)
print(f.readline())
# Still write at the end
f.write('ccccccccccc')
f.seek *****
Pointer movement in file , Only t Mode of read(n),n The number of characters represented
b Pointer movement in the schema file is in bytes
t Pattern read(n) The number of characters
with open(' The pointer moves .txt',mode='rt',encoding='utf-8')as f:
print(f.read(1))
print(f.read(1))
print(f.read(1))
print(f.read(1))
print(f.read(1))
b Pattern read(n)# b Pointer movement in the schema file is in bytes
with open(' The pointer moves .txt',mode='rb')as f:
print(f.read(1).decode('utf-8'))
print(f.read(1).decode('utf-8'))
# One third of a Chinese character ( Chinese needs 3 Bytes )
print(f.read(3).decode('utf-8'))
print(f.read(3).decode('utf-8'))
print(f.read(3).decode('utf-8'))
f.seek(offset,whence) There are two parameters :
offset: Represents... That controls the movement of the pointer
whence: Represents the number of bytes moved by reference to the location
whence = 0: Refer to the beginning of the file ( default ), special , Can be in t and b Use... In mode
whence = 1: Refer to the current location , Must be in b In mode, use
whence = 2: Refer to the end of the document , Must be in b In mode, use
t Pattern The number of bytes moved counts Read according to the characters
with open('seek.txt',mode='rt',encoding='utf-8')as f:
f.seek(2,0)
print(f.read(1))
b Pattern Number of bytes moved Read the number of bytes
with open('seek.txt',mode='rb')as f:
f.seek(2,0)
print(f.read(3).decode('utf-8'))
whence = 1: Refer to the current location , Must be in b In mode, use
whence = 2: Refer to the end of the document , Must be in b In mode, use
with open('seek.txt',mode='rb')as f:
msg = f.read(5)
print(msg.decode('utf-8'))
print(f.tell()) # Check the position of the pointer
f.seek(3,1)
print(f.read(3).decode('utf-8'))
whence = 2: Refer to the end of the document , Must be in b In mode, use
with open('seek.txt',mode='rb')as f:
f.seek(0,2)
print(f.tell()) # Check the position of the pointer
f.seek(-3, 2)
print(f.read(3).decode('utf-8'))
Method 1 for modifying files :
1 Read the contents of the file from the hard disk into the memory
2 Make changes in memory
3 Overwrite the modified result in memory and write it back to the hard disk
with open(' File modification .txt',mode='rt',encoding='utf-8')as f:
all_data = f.read()
print(all_data)
with open(' File modification .txt',mode='wt',encoding='utf-8')as f:
f.write(all_data.replace(' Red, yellow, blue ',' Buddy '))
Method 2 for modifying documents :
1 Open the source file in read mode , Open a temporary file by writing
2 Write to the temporary file after each content read from the source file is modified , Until the source file is read
3 Delete the source file , Rename the temporary file to the source file name
import os
with open(' Document revision II .txt',mode='rt',encoding='utf-8')as read_f , open(' The temporary file .txt',mode='wt',encoding='utf-8') as write_f:
for line in read_f:
write_f.write(line.replace(' Buddy ','vib'))
# File modification 2 delete
os.remove(' Document revision II .txt')
# The temporary file .txt Change to Document revision II
os.rename(' The temporary file .txt',' Document revision II .txt')
Mode one :
advantage : In the process of file modification, there is always a copy of data on the hard disk
shortcoming : Too much memory , Not for large files
Mode two :
advantage : There is only one line of the source file in memory at the same time , Not too much memory
shortcoming : In the process of file modification, the source file and temporary file coexist , There will be two copies of data on the hard disk at the same time , That is, the hard disk will be occupied too much in the process of modification
Heaven has endowed me with talents for eventual use
Japanese
with open('test.txt',mode='wt',encoding='shift_jis')as f1:
f1.write('うまれつき raw まれつきわたくし private こそかならず have to ずやく service にたつ state つ')
with open('test.txt',mode='rt',encoding='shift_jis')as f1:
a = f1.read()
print(a)
!!! Summarize two very important points !!!
1、 The core rule to keep code safe is , What standard are the characters encoded according to ,
We need to decode according to what standard , The standard here is character encoding
2、 All characters written in memory , Make no exception , All are unicode code , Let's open the editor ,
Enter a “ you ”, We can't say “ you ” It's a Chinese character , Now it's just a symbol ,
This symbol may be used in many countries , The style of this word may be different depending on the input method we use .
Only when we save to the hard disk or transfer based on the network ,
To be sure ” you “ It's a Chinese character , It's also a Japanese word , This is it. unicode The process of converting to other encoding formats
Similar to human eyes and brai
The system is divided into stu
List of articles Problem desc