Article Directory
Declare mode
‘r’ open for reading(default)
‘w’ open for writing ,truncating the file first
‘x’ create a new file and open it for writing
‘a’ open for writing,appending to the end of the file if it exists
‘b’ binary mode
‘t’ text mode(default)
‘+’ open a disk file for updating(reading and writing)
‘U’ universal newline mode(deprecated)
Read operation
readable():
stream=open(r'a.txt')
result=stream.readable()#代表能不能的意思(判断是否可以读取,True能读,flase不能读)
print(result)
read():
stream=open(r'a.txt')
container=stream.read()
print(container)
note:
open()
The default is gbk encoding, if the txt file save format is not ANSI
When reading, the following error will be generated
Then there are two solutions at this time, the
first one: set the read file encoding to ANSI,
The second: do not change the file encoding, directly add encoding='utf-8' after the open function
stream=open(r'a.txt',encoding='utf-8')
#result=stream.readable()#代表能不能的意思(判断是否可以读取,True能读,flase不能读)
line=stream.readlines()
for i in line:
print(i)
readline():
stream=open(r'a.txt')
container=stream.readline()
print(container)
stream=open(r'a.txt')
#result=stream.readable()#代表能不能的意思(判断是否可以读取,True能读,flase不能读)
while True:
line=stream.readline()
print(line)
if not line:
break
(Because there is a newline character, then after readline reads it out, another newline character is automatically added, so there are only two newline characters)
readlines():
stream=open(r'a.txt')
#result=stream.readable()#代表能不能的意思(判断是否可以读取,True能读,flase不能读)
line=stream.readlines()
print(line)
Confirms the |\n in readline
stream=open(r'a.txt')
#result=stream.readable()#代表能不能的意思(判断是否可以读取,True能读,flase不能读)
line=stream.readlines()
for i in line:
print(i)
Read:
open(path/filename,'rt')
------->Return value: stream (pipe)
container=stream.read()
---->Read the contents of the tube
Note: If the path/filename
error is passed, an error will be reported:FileNotFoundError
Read jpg
Note: The default mode of open is'rt', which means to read text documents, which means that all binary files are converted into text documents. Therefore, the default format of reading images cannot be maintained.
stream=open(r'02.jpg','rb')
line=stream.read()
print(line)
stream=open(r'02.jpg')
line=stream.read()
print(line)
It can form a stream, but it cannot be read. The reason has been explained above.
If you cannot use the default reading method for pictures, you should use mode='rb'
to sum up:
read() reads all content
readline() reads one line of content at a time
readlines() reads all lines and saves them to the list
readable() determines whether it is readable
Write file
write():
stream=open(r'a.txt','w')
s='''
欢迎来到王者荣耀
敌军还有三秒到达战场
请做好准备'''
result=stream.write(s)
print(result)
stream.close()
mode is'w', which means write operation
method:
write (content) will clear the original content every time, and then write the current content
stream=open(r'a.txt','w')
s='''小彪彪'''
result=stream.write(s)
a='comeon'
result=stream.write(a)
print(result)
stream.close()
It is not written on a new line. So how do you write in a newline?
writelines():
Can you tell from the name at a glance, write it on a new line? Actually it is not, it does not automatically wrap, it is just iterable
stream=open(r'a.txt','w')
s='''小彪彪'''
result=stream.write(s)
a='comeon'
result=stream.writelines([a,a,a,a])
print(result)
stream.close()
stream=open(r'a.txt','w')
s='''小彪彪'''
result=stream.write(s)
a='comeon'
result=stream.writelines([a+'\n',a+'\n',a+'\n',a+'\n'])
print(result)
stream.close()
In fact, it can be written like this
result=stream.writelines(['comeon\n','comeon\n','comeon\n','comeon\n'])
Don't be misled by the name of this function, it just can be added to a list
pythonwritelines(iterable)
There is no wrapping effect, you can only add it separately
In appended form, that is, mode is'a'
stream=open(r'a.txt','a')
s='''小彪彪'''
result=stream.write(s)
a='comeon'
result=stream.writelines(['comeon\n','comeon\n','comeon\n','comeon\n'])
print(result)
stream.close()
Copy operation
Copy jpg
Fixed directory
with open(r'02.jpg','rb') as stream:
container=stream.read()
with open(r'03.jpg','wb') as wstream:
wstream.write(container)
Current directory
os.path.dirname(__file__)#获取当前文件所在的文件目录 (绝对目录)
import os
with open(r'02.jpg','rb') as stream:
container=stream.read()
path=os.path.dirname(__file__)
path1=os.path.join(path,'03.jpg')
with open(path1,'wb') as wstream:
wstream.write(container)
os.path
Perform a series of operations on the things in your system to
os.path.dirname(__file__)
indicate where the directory of the current file
os.path.join(path,'')
is. The new path returned is the spliced new path because path is a directory, not a file name.
How to get the file name
stream=open(r'G:\.vs\02.jpg','rb')
line=stream.read()
print(stream.name)
file=stream.name
filename=file[file.rfind('\\')+1:]
print(filename)