DAY-08 file operation

Three kinds of strings span>

1. Ordinary string: u’Using characters as output unit’

print(u'abc') < span style="color: #008000;"># for display

2. Binary character string: b’Binary character string uses byte as output unit’

 print(b'< span style="color: #800000;">abc') # For transmission

3. Original string: r’takes characters as the output unit, and all the symbols that can be escaped in the string are here All output as is’

print< /span>(u'a	b
c') print(r'a	b
c') # Unescaping

Three steps of file operation

1. Open the file: the hard disk space is held by the operating system, and the file object is held by the application

< span style="font-size: 14pt;">f = open('source.txt', 'r ', encoding='utf-8')

2. Operation File

data = f.read()

3. Release files: release the operating system’s holding of hard disk space

f.close()

< span class="md-plain md-expand">Basic reading

f = open ('source.txt' , 'r', encoding='utf-8' ) f.read() # Read all content at once

f.read(10) # read the specified number of characters
f.readline() # read one line at a time The basis of this reading)
f.readlines() # read and save all the content, and press the line feed mark as the read One-time basis, stored as a list
f.close()

Basic writing

wf = open('target.txt', 'w', encoding=< span style="color: #800000;">'utf-8' span>) wf.write('123
') # Write one at a time, the line must be marked with 


wf.write('456 ') wf.flush() # Send a message to the operating system to refresh the data written in the memory to the hard disk
wf.write('789 ') wf.writelines([' abc ', ' def ', 'xyz ']) # Write multiple lines at once, the lines must be marked with
wf.close() # 1. Refresh the data written in the memory to the hard disk 2. Free up hard disk space

with…open syntax

Optimized and integrated the opening and release of file resources

  –The file object can be manipulated within the indentation of with. Once the indentation is cancelled, the resource will be released

< span style="font-size: 14pt;">part1 # as alias, rf holds variable of file resource

with open('target.txt', 'r', encoding='utf-8') as rf: # Specific code for file operations # Once the indentation is cancelled, the resource is released

 part2 with open('target.txt', 'r', encoding='utf-8') as rf1, open('target1.txt', 'r', encoding='utf-8') as rf2: print span>(rf1.read()) print(rf2.read()) # print(rf1.read( )) # Report an error # print(rf2.read()) # Report an error

 part3 with open('target.txt ', 'r', encoding='utf-8') as rf1: with open( span>'target1.txt ', 'r', encoding='utf-8') as rf2:  print( rf1.read()) print(rf2.read())  # print(rf1.read()) # Can be operated

# print(rf2.read()) # Cannot operate< /span>

File operation mode

1. Main mode: r | w | a | x

  –Only one main mode can be selected, which defines the main operation method

< p>2. Slave mode: t | b | +

  –Slave mode must also appear, but the number is not necessarily one, add additional functions to the master mode

< p>r: Read, must have
w: Clear to write, optional
a: Additional writing, optional
x : Create and write, must be none

t: By default, operate by character
b: Operate by byte
+: Read and write

# rt: The file must exist in advance , There is no error, the file operation is in character form-abbreviated as r # wt: file It can exist or not. If it exists, it will be written after being cleared. If it does not exist, it will be written after creating a new file. File operations are in character form-abbreviated as w # at: The file may or may not exist. The existing content is appended to the end of the previous content, and there is no write after creation. File operations are in character form-abbreviated as a

# rb: The file must exist in advance, there is no error, the file operation is in byte format # wb: The file can exist or not, if it exists, it will be cleared and written , There is no write after new creation, file operations are in byte format # ab: file It can exist or not. There is additional writing at the end of the previous content, and there is no writing after new creation. File operations are in byte form.

# r+t: The file must be readable and writable, and replace it from the beginning by default Write, operate by character # w+t: The file exists, clears, but does not exist, can be created Read and write, operate by character # a+t: file exists, append does not exist, create Readable and writable, operated by characters

# r+b: The file must be readable and writable, and replace it from the beginning by default Write, operate by byte # w+b: File exists, empty, not created Read and write, operate in bytes There are created readable and writable, byte operations

File operation encoding problem

t mode: What encoding is used in the original file? Which encoding operation to select, if not selected, it will be consistent with the operating system by default
  – In the mode, the encoding must be specified
p>

b mode: The data of the hard disk is binary, and the code can be identified according to the content, and the data when writing is also processed in advance through a certain code , There is no need to specify the code during operation

File copying

1. Copying text files: either t or b

with open('target.txt', 'r', encoding='utf-8') as rf: with open('target2.txt', 'w< /span>', encoding='utf-8') as wf: for line in rf: wf.write(line ) with open('target.txt', 'rb') as rf: with open( 'target3.txt', 'wb') as wf: for line in rf: wf.write(line)

2. Non-text files can only be operated in b mode, no encoding is required, because the encoding and decoding process is not involved

with open('001.mp4', 'rb') as rf: with open('002.mp4', 'wb') as wf: for line < span style="color: #0000ff;">in rf: wf.write(line)

Cursor operation

1. The cursor operates on bytes, so it can only be operated in b mode
2. Cursor Operation can change the operating position. In r mode, you can change the position to operate. All main modes select r mode
3.seek(offset, whence):
  —offset is an integer, which means the number of bytes to shift backwards, and a negative number means how many bytes to shift forwards< br> - whence: 0 means set the cursor to the beginning, 1 means from the current position, 2 means set the cursor to the end

# You are Japanese

with open('target.txt', 'rb') as f: #< span style="color: #008000;"> read 6 bytes first

data = f.read(6) print(data.decode( 'utf-8'< /span>)) # You are
# Offset the cursor by 3 bytes from the beginning
f.seek(3, 0) data = f.read(6) print(data.decode('utf-8')) # It’s the day
# offset 3 bytes from the current cursor position
f.seek(-3, 1) data = f.read(3) < /span>print(data.decode('utf-8')) # Day
f.seek(-3, 2) data = f.read (3) print(data.decode('utf-8')) # People

< pre>print(uabc) #< /span> For display

print(b'abc') # Used to pass 

print< /span>(u'a	b
c') print(r'a	b
c') # Unescaping

f = open('source.txt', 'r', encoding='utf-8')

data = f.read()

f.close()

f = open('< span style="color: #800000;">source.txt', '< /span>r', encoding='utf-8') f.read() # read all content at once End

f.read(10) # read the specified number of characters
f.readline() # read one line at a time The basis of this reading)
f.readlines() # read and save all the content, and press the line feed mark as the read One-time basis, stored as a list
f.close()

wf = open('target.txt', 'w', encoding='utf-8 ') wf.write('123
') # Write one at a time, the line must be marked with 


wf.write('456 ') wf.flush() # Send a message to the operating system to refresh the data written in the memory to the hard disk
wf.write('789 ') wf.writelines([' abc ', ' def ', 'xyz ']) # Write multiple lines at once, the lines must be marked with
wf.close() # 1. Refresh the data written in the memory to the hard disk 2. Free up hard disk space

part1 # as is an alias, rf holds variables for file resources

with open('target.txt', 'r', encoding='utf-8') as rf: # Specific code for file operations # Once the indentation is cancelled, the resource is released

  part2 with open('target .txt', 'r', encoding='utf-8') as rf1, open('target1.txt', 'r', encoding='utf-8 ') as rf2: print(rf1.read()) print(rf2.read() ) # print(rf1.read()) # report an error # print(rf2.read()) # Report an error

< /p>

 part3 with open(< /span>'target.txt', 'r', encoding='utf-8') as rf1: with open(' target1.txt', 'r', encoding='utf-8') as rf2: print(rf1.read()) print(rf2.read()) #< span style="color: #008000;"> print(rf1.read()) # Can be operated

# print(rf2.read()) # Cannot operate< /span>

# rt: The file must exist in advance, there is no error, the file operation is in the form of characters-abbreviated as r #  wt: The file can exist or not exist. If it exists, it will be cleared and written. If it does not exist, it will be written after creation. File operations are in character form-abbreviated as w  # at: The file can exist or not, and the existing content is appended to the end of the previous content. Write after creation does not exist, file operations are in character form-abbreviated as a


# rb: The file must exist in advance, there is no error, the file operation is in byte format # wb: The file can exist or not, if it exists, it will be cleared and written , There is no write after new creation, file operations are in byte format # ab: file It can exist or not. There is additional writing at the end of the previous content, and there is no writing after new creation. File operations are in byte form.

# r+t: The file must be readable and writable, and replace it from the beginning by default Write, operate by character # w+t: The file exists, clears, but does not exist, can be created Read and write, operate by character # a+t: file exists, append does not exist, create Readable and writable, operated by characters

# r+b: The file must be readable and writable, and replace it from the beginning by default Write, operate by byte # w+b: File exists, empty, but not created Read and write, operate in bytes There are created readable and writable, byte operations

with open( 'target.txt'< /span>, 'r ', encoding='utf-8') as rf: with open('target2.txt',  'w, encoding=utf-8) as wf: for line in rf: wf.write(line) with open(target.txt, rb) as rf: with open(target3.txt, wb) as wf: for line in rf: wf.write(line)

with open(001.mp4, rb) as rf: with open(002.mp4, wb) as wf: for line in rf: wf.write(line)

# 你是日本人

with open(target.txt, rb) as f: # 先读6个字节
data = f.read(6) print(data.decode(utf-8)) # 你是
# 将游标从头开始往后偏移3个字节
f.seek(3, 0) data = f.read(6) print(data.decode(utf-8)) # 是日
# 从当前游标位置往前偏移3个字节
f.seek(-3, 1) data = f.read(3) print(data.decode(utf-8)) #
f.seek(-3, 2) data = f.read(3) print(data.decode(utf-8)) #

Leave a Comment

Your email address will not be published.