File Handling
What is document processing
Modify the stored information
What is the file
The concept of virtual storage of information provided by the OS (binary to store information)
Process file operations
1. Open the file
f=open(r'D:\上海python12期视频\python12期视频\day 09\test.py','w',encoding='utf-8')
2. Modify / read files
f.write('''
孩儿立志出湘关,学不成名誓不还
埋骨何须桑之地,人生无处不青山
''')
3. Save the file
f.flush()#快速保存,可以不使用
4. Close the file: python by an operating system file
f.close()#告诉操作系统关闭文件
Three file open mode
Read: rt read_text read text read-only
f=open(r'D:\上海python12期视频\python12期视频\day 09\test.py','rt',encoding='utf-8')
data=f.read()#读取所有文本内容
print(data)
print(f.readable())#判断是否可读
print(f.writeable())#判断是否可写
Only for understanding
print(f.readline())#一行一行读取
print(f.readlines())#读取所有行放入列表
Cycle read out the text
for i in f.read():#循环出一个个字符
print(i)
for i in f:
print(i)#循环出一行行,每行末尾默认有一个换行
wt: Clear writing, unreadable
f.write('abc')
Only for understanding
f.writelines(['asd','dgg','werf'])#自动拼接列表元素,一行写入
at: additional writing
f=open(r'D:\上海python12期视频\python12期视频\day 09\test.py','rt',encoding='utf-8')
f.writeable('中')
Open the file in two ways
t -> Text b -> Binary mode
The two things that are not used alone, with r / w / a in combination with, no encoding
b mode may be used to save the image / audio / video
Absolute and relative paths
Path of the file: the file on the hard disk of address
Absolute path
From the beginning of the letter D: \ Shanghai python12 of video \ python12 of video \ day 09 \ test.py
relative path
Executable file (currently running file) folder under the file name of the executable file and open files belonging to the same folder
Try to use a relative path to the future
f=open('test.py','r',encoding='utf-8')
print(f.read())
with file management context
provided with an automatic shut down file (ending the occupation of the operating system)
with open('test.py','r',encoding='utf-8') as f:
#在这个缩进下不会关闭文件,在这个缩进下对文件操作
data=f.read()#data放在python的内存中
print(data)#关闭文件(操作系统),没有关闭Python内存中的文件
print(f)#只涉及python
print(f.read())#涉及操作系统,报错
Advanced application file
R + readable writable
with open('test.py','r+',encoding='utf-8') as fr:
print(fr.readable())
print(fr.writeable())
fr.write('高级')#光标在文件头部,覆盖后面的字符
Readable and writable w + w no difference
with open('test.py','w+',encoding='utf-8') as fr:
print(fr.readable())
print(fr.writeable())
A + a read-write mode, the default cursor at the end
with open('test.py','a+',encoding='utf-8') as fr:
print(fr.readable())
print(fr.writeable())
To sum up: If you really have to have read writable demand, open the file in two different modes
The cursor advanced applications (move the cursor)
Eight binary bits to a byte, three 8-bit hexadecimal form a character (for Chinese, English is a one byte character)
At three positions to move the cursor (in bytes)
seek
with open('test.py', 'rb') as fr:
fr.seek(5) # 移动了3个字节,一个中文,默认从开头开始 print(fr.read()) print(fr.read().decode('utf8'))
whence provides that only three modes 0, 1, # 0 corresponds to the beginning of the file header; 1 corresponds to the position of the current file; 2 corresponds to the end of file
fr.seek(3, 0) #0从开头 print(fr.read().decode('utf8'))
fr.seek(3, 1)
print(fr.read().decode('utf8'))
fr.seek(0, 2)
print(fr.read())
tell: tell your current location
with open('test.py', 'rb') as fr:
fr.seek(3, 0)
print(fr.tell())
truncate: Truncated
with open('test.py', 'ab') as fa:
fa.truncate(2)
Move the cursor (in characters) read
with open('test.py', 'r', encoding='utf8') as fr:
print(fr.read(3)) # n表示n个字符,不加默认读取所有 # 中文和英文都属于一个字符
Modify the file
Do not modify a file that only cover
Principle cache files: open multiple files
import os
with open('test.py','r',encoding='utf-8') as fr,\
open('test_swap.py','w',encoding='utf-8') as fw:
data=fr.read()
data=data.replace('sb','傻逼')
fw.write(data)
os.remove('test.py')
os.rename('test_swap.py','test.py')
If the file is too large
import os
with open('test.py','r',encoding='utf-8') as fr,\
open('test_swap.py','w',encoding='utf-8') as fw:
for i in fr:
s='傻逼'
fw.write('sb',s)
fw.flush#保存
os.remove('test.py')
os.rename('test_swap.py','test.py')