python day08 file operation

Keywords: Python encoding network

Summary of 2010.4.8 S21 day08 Notes

I. system

For computers, whether it is file storage/network transmission, data is essentially binary (0101010101), such as: video/picture/file stored on computers are binary; expressions/text/language/video sent by QQ/Wechat chat are all binary.

Radix:

  • Binary system, internal computer.
  • 8 binary system
  • In general, the computer can acquire the decimal system by using the decimal system, and then automatically convert it into the binary system and operate it internally.
  • Hexadecimal, commonly used to represent binary (more data with shorter content), the first edition is: x start.
Binary system Octal number system Decimal system Hexadecimal
1 1 1 1
10 2 2 2
11 3 3 3
100 4 4 4
101 5 5 5
110 6 6 6
111 7 7 7
1000 10 8 8
1001 11 9 9
1010 12 10 a
1011 13 11 b
1100 14 12 c
1101 15 13 d
1110 16 14 e
1111 17 15 f
10000 20 16 10

II. Document Operation (Supplementary)

# Open file
f = open('File path to open',mode='r/w/a',encoding='Coding defined when a file was originally written')

# operation
data = f.read()  # Read the contents of the file into memory (all)
f.write('What to write')

# Close file
f.close()
# Example 1: Generally used for text writing.
f = open('a.txt',mode='w',encoding='utf-8')
# a. Convert "Hello" to encoding codes: 
#       "Hello" - -> 10001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000
# b. Write binary into a file.
f.write('Hello') # w Opens the file, write passes in the string
f.close()

# Example 2: Generally used for picture/audio/video/unknown coding 
f = open('a.txt',mode='wb')
# f.write('Hello')  # When wb opens the file, write passes in binary, but'hello'is a string, not binary, which needs to be converted before writing. 
# 1. Convert the string to binary
data = "I am sleepy"
content = data.encode('utf-8') # Converting strings to binary by utf-8 encoding
# 2. Write the binary into the file
f.write(content) # When wb opens the file, write passes in binary
f.close()

1. File operation

  • Pattern
    • R/w/a [read-only and write-only strings]***
    • R+/w+/a+ [Readable and Writable Strings]
    • Rb/wb/ab [read-only binary] ***
      • String conversion "binary":
        v = "hello"
        data = v.encode('utf-8')
      • Binary to String Conversion
        v = b'\xe6\x88\x91\xe5\xa5\xbd\xe5\x9b\xb0'
        data = v.decode('utf-8')
    • r+b / w+b / a+b [readable and writable binary]
  • See (cursor byte position), regardless of whether the mode has b, is processed in bytes.

    obj = open('a.txt',mode='r',encoding='utf-8')
    obj.seek(3) # Jump to the specified byte position
    data = obj.read()
    obj.close()
    
    print(data)
    
    
    obj = open('a.txt',mode='rb')
    obj.seek(3) # Jump to the specified byte position
    data = obj.read()
    obj.close()
    
    print(data)
  • tell(), which gets the current byte position of the cursor

    obj = open('a.txt',mode='rb')
    # obj.seek(3) # Jump to the specified byte position
    obj.read()
    data = obj.tell()
    print(data)
    obj.close()
  • flush, forcing data in memory to be written to the hard disk

    v = open('a.txt',mode='a',encoding='utf-8')
    while True:
        val = input('Please input:')
        v.write(val)
        v.flush()
    
    v.close()

2. Close file operations

Artistic young people

v = open('a.txt',mode='a',encoding='utf-8')

v.close()

Asshole;fucker;stupid

with open('a.txt',mode='a',encoding='utf-8') as v:
    data = v.read()
    # Close the file automatically after the indented code has been executed

3. Modification of File Content

with open('a.txt',mode='r',encoding='utf-8') as f1:
    data = f1.read()
new_data = data.replace('Flying sprinkles','666')

with open('a.txt',mode='w',encoding='utf-8') as f1:
    data = f1.write(new_data)

Large Document Modification

f1 = open('a.txt',mode='r',encoding='utf-8')
f2 = open('b.txt',mode='w',encoding='utf-8')

for line in f1:
    new_line = line.replace('Asse','Death!')
    f2.write(new_line)
f1.close()
f2.close()
with open('a.txt',mode='r',encoding='utf-8') as f1, open('c.txt',mode='w',encoding='utf-8') as f2:
    for line in f1:
        new_line = line.replace('Asse', 'Death!')
        f2.write(new_line)

Posted by fatal on Wed, 24 Apr 2019 14:57:34 -0700