Python basic IO operations

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)

Insert picture description here

read():

stream=open(r'a.txt')
container=stream.read()
print(container)

Insert picture description here

note:

open()The default is gbk encoding, if the txt file save format is not ANSI
Insert picture description here

When reading, the following error will be generated
Insert picture description here

Then there are two solutions at this time, the
first one: set the read file encoding to ANSI,
Insert picture description here

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)

Insert picture description here

readline():

stream=open(r'a.txt')
container=stream.readline()
print(container)

Insert picture description here

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)

Insert picture description here

readlines():

stream=open(r'a.txt')
#result=stream.readable()#代表能不能的意思(判断是否可以读取,True能读,flase不能读)

line=stream.readlines()
print(line)

Insert picture description here

Confirms the |\n in readline

stream=open(r'a.txt')
#result=stream.readable()#代表能不能的意思(判断是否可以读取,True能读,flase不能读)

line=stream.readlines()
for i in line:
    print(i)

Insert picture description here

Read:
open(path/filename,'rt')------->Return value: stream (pipe)

container=stream.read() ---->Read the contents of the tube

Note: If the path/filenameerror 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)

Insert picture description here

stream=open(r'02.jpg')

line=stream.read()
print(line)

Insert picture description here

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()

Insert picture description here

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()

Insert picture description here

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()

Insert picture description here

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()

Insert picture description here

In fact, it can be written like this

result=stream.writelines(['comeon\n','comeon\n','comeon\n','comeon\n'])

Insert picture description here

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()

Insert picture description here

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)

Insert picture description here

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)
        

Insert picture description here
os.pathPerform 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)

Insert picture description here

Guess you like

Origin blog.csdn.net/CSNN2019/article/details/114558114