Python Introduction Document

1. File operation

Find the file location

Double-click Open

Do some work

r-read (read) w-write (write) a-addition
rd -- read byte wd -- empty write, write section ad -- add write (byte)

r+ -- Read and Write w+ -- Write and Read a+ -- Additional Reading

Close files

open(): Open. Open controls the operating system through python to open files.

Open ("t1", "r", "encoding="utf-8")//Open ("path of file","operation performed","encoding form of file")

File file location

Model defaults to r

Encoding of encoding file

f file handle

Files can only be read once

(1) Read operation

<1> R -- Reading

f = open("D:\Python_s25\day08\Miss Sister's Telephone Number",mode="r",encoding="utf-8")print(f.read())   # All read
print(f.read(3))  # Read by character
print(f.readline())  # The default tail has an
print(f.readline().strip())  # Read a line
print(f.readline().strip())  # Remove n
print(f.readlines()) #Read line by line and store all in the list

Route:

Absolute Path: Find from Disk (c Disk)

Relative Path: Find relative to a path file

Conversion of Path:

1."D:\\Python_s25\\day08\\t1"
2.r"D:\Python_s25\day08\t1"   -- Recommended use

<2> RB -- Read bytes:

f = open("timg.jpg",mode="rb")
print(f.read())      # All read
print(f.read(3))     # Read by byte
print(f.readline())  # Read by line
print(f.readlines())

The difference between r and rb:

1.r needs to specify encoding,rb does not need (byte operation, encoding cannot be specified)

2. read(3) in R mode reads by character, read(3) in rb mode reads by byte

read and readlines can cause memory overflow if the file is large

Solution: Use the for loop to read when the file is large (just to show)

Interview questions:

f = open('t1',mode="r",encoding="utf-8")
    for i in f:   
        print(i.strip())

(2) Write operations:

<1> W - Clear Writing (Writing Text)

Clean the file first (empty it when you open it)
Write content

When the mode is w and a, the current file is used when there are files, and a file is created when there are no files.

ff = open("a1",mode="w",encoding="utf-8")
ff.write("123")  # Written content must be a string
ff = open("a1",mode="w",encoding="utf-8")
ff.write("I am a string")  # Written content must be a string
ff = open("a1",mode="w",encoding="utf-8")
ff.write("[1,2,3,4]\n")  # Written content must be a string
ff.write('1111\n')  # Written content must be a string
ff.write('2222\n')  # Written content must be a string
ff = open("a1",mode="w",encoding="utf-8")
ff.write("[1,2,3,4]\n")  # Written content must be a string
ff.write('1111\n')  # Written content must be a string
ff.write('2222\n')  # Written content must be a string

<2> WB - Clear Writing (Writing Section)

f = open('timg.jpg',mode="rb")
f1 = open("g1.jpg",mode="wb")
content = f.read()
f1.write(content)

(3) Additional operations:

<1> a -- Additional writing (text)

f = open("b1",mode="a",encoding="utf-8")
f.write("How do you do\n")
f.write("I'm fine.\n")
f.write("How is he?\n")
f.write("Hello, everyone.\n")

<2> AB -- Additional write (bytes)

(4) +Operation:

<1> R + Read and Write (a little useful)

pit -- Error in use
f = open("b1",mode="r+",encoding="utf-8")
f.write("Today is Monday")    # write is empty
print(f.read())
//Correct operation:
f = open("b1",mode="r+",encoding="utf8")
print(f.read())
f.write("Today is Monday")

<2> W + Write and Read (a little useful)

f = open("b1",mode="w+",encoding="utf-8")
f.write("Today is Monday")
f.seek(0)  # Move Cursor
print(f.read())
f = open("b1",mode="w+",encoding="utf-8")
f.write("Today is Monday")
f.seek(0)  # Move Cursor
f.write("Ah Ah Ah Ah Ah Ah Ah Ah")
f.seek(0)
print(f.read())

<3> a+Additional Reading

f = open("b1",mode="a+",encoding="utf-8")
f.write("Today is Monday")
f.seek(0)  # Move Cursor
f.write("Ah ah ah ah ah ah")
print(f.read())

(5) Other operations:

< 1 > Moving cursor: seek()

seek() Move Cursor
f.seek(0,0)  # Move the cursor to the head of the file
f.seek(0,1)  # Move the cursor to the current position
f.seek(0,2)  # Move the cursor to the end of the file
f.seek(6)   # Move 6 bytes. The cursor moves in bytes

f = open("a1","r",encoding="utf-8")
print(f.read(5))
f.seek(0,0)  # Move the cursor to the head of the file
f.seek(0,1)  # Move the cursor to the current position
f.seek(0,2)  # Move the cursor to the end of the file
print(f.read())
f = open("c1","r",encoding="gbk")
f.seek(6)   # The cursor moves in bytes
print(f.read(3))

<2> View Cursor: tell -- Look Cursor

tell Cursor
f = open("c1","r",encoding="gbk")
print(f.read(3))
print(f.tell())  # Computation in bytes

<2> Modify File: import os# Operating System Interaction Interface

f = open('a2',"r",encoding="utf-8")
f1 = open("a1","w",encoding="utf-8")
for i in f:    
    i = i.replace("day","day")                                  
    f1.write(i) 
f.close()
f1.close()
os.remove("a2")   # Delete cannot be retrieved
os.rename("a1","a2")

f = open('a2',"r",encoding="utf-8")
f1 = open("a1","w",encoding="utf-8")
for i in f:    
    i = i.replace("day","day")  
    f1.write(i)
f.close()
f1.close()
os.rename("a2","a3")
os.rename("a1","a2")

<3>With open

Automatic Closing of Files
Operating multiple files at the same time

Examination Points:

import os  # Operating System Interactive Interface
f = open('a2',"r",encoding="utf-8")
f1 = open("a1","w",encoding="utf-8")
i = f1.read().replace("day","day")    # Reading all the contents of a file can easily lead to memory overflow f1.write(i)
f.close()
f1.close()
os.rename("a2","a3")
os.rename("a1","a2")
with open("a3","r",encoding="utf-8")as f,\                      
        open('a2',"r",encoding="utf-8")as f1:    
    print(f.read())    
    print(f1.read())

2. Purpose of file operation:

Persistence: Permanent Storage

Tags: Python encoding

Posted on Wed, 28 Aug 2019 21:47:41 -0700 by mevasquez