Getting Started with Python's file

Getting Started with Python's file

1, file operations

Locate the file location

Double-click to open

Do something

r-read (read) w-write (write) a- added
rd-- read byte wd-- empty write, write byte ad - additional write (byte)

r + - reader w + - W R a + - additional reading

Close the file

open (): open, open python controlled by the operating system to open the file

open ( "t1", "r", encoding = "utf-8") // Open ( "path file", "operation" and "file encoded form")

Location file file

The default mode is to not write r

Encoding encoding files

f file handle

Files can only be read once

(1) read operation

<1> r - Read

f = open("D:\Python_s25\day08\小姐姐电话号",mode="r",encoding="utf-8")print(f.read())   # 全部读取
print(f.read(3))  # 按照字符读取
print(f.readline())  # 默认尾部有一个\n
print(f.readline().strip())  # 读取一行
print(f.readline().strip())  # 将\n去除
print(f.readlines()) #一行一行读取,全部存储在列表中

path:

Absolute path: Find Disk (c disk) to start

Relative path: relative to find a path to the file

Escape route:

1."D:\\Python_s25\\day08\\t1"
2.r"D:\Python_s25\day08\t1"   -- 推荐使用

<2> rb - read byte:

f = open("timg.jpg",mode="rb")
print(f.read())      # 全部读取
print(f.read(3))     # 按照字节读取
print(f.readline())  # 按照行进行读取
print(f.readlines())

r and rb difference:

1.r specify encoding, rb not required (byte operation, can not be specified encoding)

2.r mode read (3) according to the character reading, rb mode read (3) read in bytes

read and readlines If the file is large, there will be a memory overflow

Solution: When a file is large, use a for loop to read (just show a bit)

Interview questions:

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

(2) writes:

<1> w - Clear Write (write text)

First empty the file (be empty when you open the file)
Write to

When the mode is w and a, there is a file with the current file, no file is created a file

ff = open("a1",mode="w",encoding="utf-8")
ff.write("123")  # 写的内容必须是字符串
ff = open("a1",mode="w",encoding="utf-8")
ff.write("我是一个字符串串")  # 写的内容必须是字符串
ff = open("a1",mode="w",encoding="utf-8")
ff.write("[1,2,3,4]\n")  # 写的内容必须是字符串
ff.write('1111\n')  # 写的内容必须是字符串
ff.write('2222\n')  # 写的内容必须是字符串
ff = open("a1",mode="w",encoding="utf-8")
ff.write("[1,2,3,4]\n")  # 写的内容必须是字符串
ff.write('1111\n')  # 写的内容必须是字符串
ff.write('2222\n')  # 写的内容必须是字符串

<2> wb - Clear Write (write byte)

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

(3) add operation:

<1> a - Write Append (text)

f = open("b1",mode="a",encoding="utf-8")
f.write("你好啊\n")
f.write("我好啊\n")
f.write("他好啊\n")
f.write("大家好啊\n")

<2> ab - additional write (byte)

(4) + action:

<1> r + write (Somewhat)

坑 -- 使用方式是错误
f = open("b1",mode="r+",encoding="utf-8")
f.write("今天是周一")    # write是清空写
print(f.read())
正确的操作:
f = open("b1",mode="r+",encoding="utf8")
print(f.read())
f.write("今天是周一")

<2> w + W R (Somewhat)

f = open("b1",mode="w+",encoding="utf-8")
f.write("今天是周一")
f.seek(0)  # 移动光标
print(f.read())
f = open("b1",mode="w+",encoding="utf-8")
f.write("今天是周一")
f.seek(0)  # 移动光标
f.write("啊啊啊啊啊啊")
f.seek(0)
print(f.read())

<3> a + additional read

f = open("b1",mode="a+",encoding="utf-8")
f.write("今天是周一")
f.seek(0)  # 移动光标
f.write("啊啊啊啊")
print(f.read())

(5) Other actions:

<1> Move cursor: seek ()

seek() 移动光标
f.seek(0,0)  # 移动光标到文件的头部
f.seek(0,1)  # 移动光标到当前位置
f.seek(0,2)  # 移动光标到文件末尾
f.seek(6)   # 移动6个字节。光标是按照字节移动

f = open("a1","r",encoding="utf-8")
print(f.read(5))
f.seek(0,0)  # 移动光标到文件的头部
f.seek(0,1)  # 移动光标到当前位置
f.seek(0,2)  # 移动光标到文件末尾
print(f.read())
f = open("c1","r",encoding="gbk")
f.seek(6)   # 光标是按照字节移动
print(f.read(3))

<2> View Cursor: tell - Charles cursor

tell 查光标
f = open("c1","r",encoding="gbk")
print(f.read(3))
print(f.tell())  # 按照字节进行计算

<2> to modify the file: import os # interact with the operating system interface

f = open('a2',"r",encoding="utf-8")
f1 = open("a1","w",encoding="utf-8")
for i in f:    
    i = i.replace("日","天")                                  
    f1.write(i) 
f.close()
f1.close()
os.remove("a2")   # 删除不能找回
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("天","日")  
    f1.write(i)
f.close()
f1.close()
os.rename("a2","a3")
os.rename("a1","a2")

<3>With open

Automatically closes the file
Multiple files at the same time

Test sites:

import os  # 操作系统交互的接口
f = open('a2',"r",encoding="utf-8")
f1 = open("a1","w",encoding="utf-8")
i = f1.read().replace("天","日")    # 将文件中全部内容读取 容易导致内存溢出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, file manipulation purposes:

Persistence: persistent storage

Guess you like

Origin www.cnblogs.com/caiyongliang/p/11428830.html