Learning Python file operation two

1, file mode

x mode (a mode of operation control file)
x:
  write-only mode, unreadable;
  it does not exist is created, then there is an error.
File read and write the contents of the control mode 
T:
1, are read as a string (Unicode) of the unit
2, the text file only for
3 must be specified character encoding, which must specify the encoding parameter
b: binary mode
1, the reader are in bytes
2, for all files can be
3, must not specify the character encoding that must not specify the encoding parameters

summary:
1, in a plain text file operation mode t help us save a link encoding and decoding, b mode which requires manual coding and decoding, so that this time t is more convenient mode
2, mode b for use only non-text files (such as images, video, audio, etc.)
b Mode Applications:
# File copy tool 
in SRC_FILE of INPUT = ( ' source file path >>: ' ) .strip () 
and dst_file = INPUT ( 'target file path >>: ' ) .strip () 
with Open (R & lt ' {} ' .format (in SRC_FILE of ), MODE = ' RB ' ) AS F1, \ 
    Open (R & lt ' {} ' .format (and dst_file), MODE = ' WB ' ) AS F2:
     # RES = f1.read () is too large memory footprint # 
    # F2. the Write (RES) 

    for Line in f1: 
        f2.write (Line)

2. Other methods of file operations

2.1 read operation associated

2.1.1 loop reads the file

mode 1. read data line by line for line in f a ( when a single line of data will cause the amount of time into the content over a long time large ).
#1
with open(r'g.txt',mode='rt',encoding='utf-8') as f:
    for line in f:
        print(len(line),line)
#2
with open(r'g.txt',mode='rb') as f:
    for line in f:
        print(line)
#3
with open(r'test.jpg',mode='rb') as f:
    for line in f:
        print(line)

 

2 embodiment. Own control amount of data each time data is read

with open(r'test.jpg',mode='rb') as f:
    while True:
        res=f.read(1024) # 1024
        if len(res) == 0:
            break
        # if not res:
        #    break
        print(len(res))

 

2.1.2f.readline and f.readlines 
  
readline: a read line
with open(r'g.txt',mode='rt',encoding='utf-8') as f:
    # res1=f.readline()
    # res2=f.readline()
    # print(res2)

    while True:
        line=f.readline()
        if len(line) == 0:
            break
        print(line)

 

  readlines: The content read into memory all at once, and generates a list of dividing each line '\ n'. (Emphasize: f.read () and f.readlines () are read into memory the contents of a one-time, if the content over the General Assembly cause a memory overflow)
with open(r'g.txt',mode='rt',encoding='utf-8') as f:
    res=f.readlines()
    print(res)

 

2.2 write-related operations

f.writelines (): a string sequence can be written to the file, the character string of this sequence may be generated by an iterative object.
with open('h.txt',mode='wt',encoding='utf-8') as f:
    # f.write('1111\n222\n3333\n')

    # l=['11111\n','2222','3333',4444]
    l=['11111\n','2222','3333']
    # for line in l:
    #     f.write(line)
    f.writelines(l)


with open('h.txt', mode='wb') as f:
    L = [#
    #      '1111aaa1 \ n'.encode (' UTF-. 8 '), 
    #      ' 222bb2'.encode ( 'UTF-. 8'), 
    #      '33eee33'.encode (' UTF-. 8 ') 
    # ] 

    # added 1: if pure English characters can be directly obtained prefix bytes type b 
    # L = [ 
    #      b'1111aaa1 \ n-', 
    #      b'222bb2', 
    #      b'33eee33 ' 
    # ] 

    # supplement 2:' on '.encode (' utf 8 ') is equivalent to the bytes (' on ', encoding =' UTF-. 8 ') 
    L = [ 
        bytes ( ' on ah ' , encoding = ' UTF-. 8 ' ), 
        bytes ( 'Punch die ' , encoding = ' UTF-. 8' ), 
        Bytes ( ' brothers ' , encoding = ' UTF-. 8 ' ), 
    ] 
    f.writelines (L)

 

f.flush: writing data to the hard disk immediately.
with open('h.txt', mode='wt',encoding='utf-8') as f:
    f.write('')
    # f.flush()

 

2.3 Other

with open('h.txt', mode='wt',encoding='utf-8') as f:
    print(f.readable()) 
    print(f.writable())
    print(f.encoding)
    print(f.name)

print(f.closed)

3. The operation of the Advanced control file pointer
pointer movement unit is in bytes / bytes. Only one special case: Read (n-) in t pattern, n represents the number of characters
with open('aaa.txt',mode='rt',encoding='utf-8') as f:
    res=f.read(4)
    print(res)

f.seek (n, mode): n refers to the number of bytes moved
# Mode: 
# Mode 0: a reference file headers 
f.seek (. 9 , 0) 
f.seek ( 3,0) # . 3 

# Mode 1: a reference current pointer location 
f.seek (9,1 ) 
f.seek ( 3,1) # 12 is 

# mode 2: reference position is the end of the file should be moved backwards 
f.seek (-9,2) # . 3 
f.seek (-3,2) # . 9 

# emphasized: only mode may be used at 0 t, 1,2 must be used in the mode b

 

f.tell (): Gets the current file pointer position
with open('aaa.txt',mode='rb') as f:
    f.seek(9,1)
    f.seek(3,1) # 12
    print(f.tell())
 
 

Guess you like

Origin www.cnblogs.com/imark7/p/12506114.html
Recommended