Python basic study notes 07-file

1. Documents

1.1 File operation steps
Open:
Syntax: open (name, mode)
name: the string of the target file name to be opened
mode: Set the access mode of the opened file: read-only, write, append

Write:
Syntax: file object.write("content")

Read:
read():
Syntax: file object. read(num)
num indicates the length of the data to be read from the file. If num is not passed in, it means that the entire file is read.
readlines(): the entire file is read by
line The content in the file is read at once, and a list is returned, where the data in each row is an element

Close:
Syntax: file object.close()

f = open('test.txt','w')    # 打开文件
f.write('hello')
f.close()

1.2 Access mode
r: Open the file in read-only mode. The pointer of the file is placed at the beginning of the file.
rb: Open a file in binary format for read-only. The file pointer will be placed at the beginning of the file
r+: Open a file for reading and writing. The file pointer will be placed at the beginning of the file
rb+: Open a file in binary format for reading and writing. The pointer of the file will be placed at the beginning of the file
w: Open a file for writing only. If the file already exists, open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file
wb: open a file in binary format only for writing. If the file already exists, open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file
w+: open a file for reading and writing. If the file already exists, open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file
wb+: Open a file in binary format for reading and writing. If the file already exists, open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file
a: Open a file for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written after the existing content. If the file does not exist, create a new file for writing.
ab+: Open a file in binary format for reading and writing. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written after the existing content. If the file does not exist, create a new file for writing.

f = open('test.txt','a')
f.write(' world')
f.close()

1.3 seek()
function: used to move the file pointer.
Syntax: file object. seek (offset, starting position)
starting position:
0: the beginning of the file
1: the current position
2, the end of the file

1.4 File backup
1. Receive the file name entered by the user
2. Plan the name of the
backup file 3. Write the backup file to the data

old_name = input("请输入备份文件名:")
# 提取后缀,找到"."
index = old_name.rfind(".")
new_name = old_name[:index]+"[备份]"+old_name[index:]
old_f = open(old_name,'rb')
new_f = open(new_name,'wb')     # 打开新旧文件
while True:
    con = old_f.read(1024)
    if len(con) == 0:
        break           
    new_f.write(con)            # 写如数据
old_f.close()
new_f.close()

1.5 File operation function
import os
os.rename("old file name", "new file name") modify file name
os.remove("target file name") delete file
os.mkdir("folder name") create folder
os.rmdir("folder name") delete the folder
os.getcwd() get the current directory
os.chdir("directory") change the default directory
os.listdir("directory") get the directory list

Guess you like

Origin blog.csdn.net/qq_44708714/article/details/105033679