File and directory operations in Python

File and directory operations in Python

Data stored in variables, sequences, and objects is temporary and is lost when the program ends. In order to save the data in the program for a long time, it is necessary to save the data in the program to a disk file. Python provides built-in file objects and built-in modules for operating files and directories. Through these technologies, data files can be easily saved in files to achieve the purpose of long-term data preservation.
① Master how to create, open and close files
② Master how to write and read file content
③ Understand os and os.path modules
④ Master how to create and delete directories
⑤ Master how to use the raise statement to throw exceptions

1. Basic file operations

In Python, there is a built-in file object. When using a file object, you first need to create a file object through the built-in open() method, and then perform some basic file operations through the methods provided by the object. 1.1 Creating and opening files
in
Python , if you want to operate a file, you need to create or open the specified file first and create a file object. The syntax format of the open function is:

file = open(filename[,mode[,buffering]])

Among them, mode is an optional parameter, which is used to specify the opening mode of the file; buffering is also an optional parameter, which is used to specify the buffer mode of reading and writing files. The value 0 means no buffering, and the value 1 means caching. If greater than 1, it means The size of the buffer.
1.1 When opening a file that does not exist, create the file first
For example: create and open a file that records the dynamics of the ant farm.

print("\n","="*10,"蚂蚁庄园动态","="*10)
file = open('message.txt','w')   # 创建或打开保存蚂蚁庄园动态信息的文件
print("\n 即将显示……\n")

(2) Open files in binary form
Using the open() function, you can not only open text files in text form, but also open non-text files in binary form,

file = open('picture.png'.'rb')
print(file)

(3) Specify the encoding method when opening the file
. When using the open() function to open the file, the GBK encoding is used by default. When the opened file is not GBK encoding, an exception will be thrown. You can add the encoding='utf-8' parameter You can specify the encoding as UTF-8.

1.2 Close the file
After opening the file, it needs to be closed in time to avoid unnecessary damage to the file. Closing the file can be achieved by using the close() method of the file object. The syntax format is:

file.close()

1.3 Use the with statement when opening the file.
After opening the file, it needs to be closed in time. When using the with statement, no matter whether an error is thrown or not, it can ensure that the opened file is closed after the with statement is executed. The syntax format is:

with expression as target
	with-boby

Among them, expression is used to specify an expression; target is used to specify a variable; with-boby is used to specify the body of the with statement.
1.4 Write the content of the file
In Python, the with() method is provided to write the content to the file. The syntax of the write() method is as follows,

file.write(string)

For example: write a piece of information to the dynamic column of Ant Manor.

print("\n","="*10,"蚂蚁庄园动态","="*10)
file = open('message.txt','w')   # 创建或打开保存蚂蚁庄园动态信息的文件
# 写入一条动态信息
file.write("你使用了1张加速卡,小鸡撸起袖子开始双手吃饲料,进食速度大大加快。\n")
print("\n 写入了一条动态……\n")
file.close()    # 关闭文件对象

insert image description here
1.5 Reading a file
After opening a file in Python, in addition to writing or appending content to it, you can also read the content in the file. Reading the content of the file is mainly divided into the following situations, 1.5.1 Reading the specified
character
file The object provides the read() method to read the specified number of characters, and its grammatical format is:

file.read([size])

Among them, file is the opened file object; size is an optional parameter, used to specify the number of characters to be read, if omitted, all content will be read at once.
For example, to read the first 9 characters in the message.txt file, you can use the code below.

with open('message.txt','r') as file:  #打开文件
		string = file.read(9)          #读取前9个字符
		pring(string)

When using the read(size) method to read a file, it is read from the beginning of the file. If you want to read part of the content, you can first use the seek() method of the file object to move the file pointer to a new position, and then apply The read(size) method reads. The basic syntax format of the seek() method is:

file.seek(offset[,whence])

Among them, file indicates the file object that has been opened, offset indicates the number of characters used to specify the movement, and its specific position is related to whence, whence is used to specify the position from which to start calculation, a value of 0 indicates that the calculation starts from the beginning of the file, and 1 indicates that Counting from the current position, 2 means counting from the end of the file, the default is 0.
For example: display the dynamics of the ant farm.

print("\n","="*25,"蚂蚁庄园动态","="*25,"\n")
with open('message.txt','r') as file:   # 打开保存蚂蚁庄园动态信息的文件
    message = file.read()    # 读取全部动态信息
    print(message)           # 输出动态信息
    print("\n","="*29,"over","="*29,"\n")

The running result is:
insert image description here1.5.2 Read a line
When using the read() method to read a file, if the file is very large, reading all the content into the memory at one time may easily cause insufficient memory, so it is usually read line by line, and the file object The readline() method is provided to read one line of data at a time. The basic syntax of the readline() method is as follows:

file.readline()

Among them, file is the opened file object. Like the read() method, when opening the file, you also need to specify the open mode as r (read-only) or r+ (read-write).
For example: display the dynamics of the ant farm line by line,

print("\n","="*35,"蚂蚁庄园动态","="*35,"\n")
with open('message.txt','r') as file:   # 打开保存蚂蚁庄园动态信息的文件
    number = 0   # 记录行号
    while True:
        number += 1
        line = file.readline()
        if line =='':
            break    # 跳出循环
        print(number,line,end= "\n")  # 输出一行内容
print("\n","="*39,"over","="*39,"\n")

The running results are as follows:
insert image description here1.5.3 Read all lines
The function of reading all lines is similar to calling the read() method without specifying a size, except that when reading all lines, a list of strings is returned, and each element is a file A line of content. To read all lines, use the readlines() method of the file object, and its syntax format is:

fle.readlines()

Among them, file is the opened file object. Like the read() method, when opening the file, you also need to specify the open mode as r (read-only) or r+ (read-write).

2. Directory operation

Directories are also called folders, which are used to store files hierarchically. Through directories, files can be stored in categories. We also know that we can quickly find the desired files through directories. In Python, there is no function or object for directly manipulating directories. , but needs to be implemented using the built-in os and os.path modules.

2.1 os and os.path modules

In Python, the built-in os module and its submodule os.path are used to operate on directories or files. When using the os module or os.path module, you need to use the import statement to import it before you can use them to provide function or variable.
Directory-related functions provided by the os module

function illustrate
getcwd return the current working directory
listdir(path) Returns the file and directory information under the specified path
mkdir(path [,mode]) Create a directory
makedirs(path1/path2…[,mode]) Create multi-level directories
rmdir(path) delete directory
removedirs(path1/path2) Delete multi-level directories
chdir(path) Set path to the current working directory
walk(top[,topdown[,onerror]]) Number of directories traversed
2.2 Path

A string used to locate a file or directory is called a path. During program development, two paths are usually involved, one is a relative path and the other is an absolute path.
2.2.1 Relative path
Before learning the relative path, you need to know what is the current working directory. The current working directory refers to the directory where the current file is located. In Python, the current working directory can be obtained through the getcwd() function provided by the os module. For example, the syntax format is:

import os
print(os.getcwd())  #输出当前目录

The relative path depends on the current working directory. If there is a file named message.txt in the current working directory, then when opening this file, you can directly write the file name, and the relative path is used at this time.
2.2.2 Absolute path
The absolute path refers to specifying the actual path of the file when using the file. It does not depend on the current working directory. In Python, the absolute path of a file can be obtained through the abspath() function provided by the os.path module. The basic syntax format of the abspath() function is as follows:

os.path.abspath(path)

Among them, path is the relative path to obtain the absolute path, which can be a file or a directory.
2.2.3 Joining paths
If you want to join two or two paths together to form a new path, you can use the join() function provided by the os.path module. The basic syntax of the join() function is as follows:

os.path.join(path1[,path2[,...]])

Among them, path1 and path2 are used to represent the file paths to be spliced, and these paths are separated by commas. If there is no absolute path in the path to be spliced, then the final spliced ​​out will be a relative path.

2.3 Determine whether the directory exists

In Python, sometimes it is necessary to determine whether a given directory exists. At this time, you can use the exists() function provided by the os.path module. The basic syntax of the exists() function is as follows:

os.path.exists(path)

Among them, path is the directory to be judged, which can be an absolute path or a relative path.

2.4 Create directory

In Python, the os module provides two functions for creating directories, one for creating a first-level directory and the other for creating a multi-level directory. 2.4.1 Creating a first-level directory Creating a first-level
directory
means that only one In Python, it can be implemented by using the mkdir() function provided by the os module. Only the last level directory of the specified path can be created through this function. If the upper level of the directory does not exist, an exception will be thrown. The syntax format for:

os.mkdir(path,mode=0o77)

Among them, path indicates the directory to be created, and can use an absolute path or a relative path, and mode indicates a numerical mode for specifying, and the default value is 0777.
2.4.2 Create multi-level directories
Use the mkdir() function to only create one-level directories. If you want to create multi-level directories, you can use the makedirs() function of the os module. This function is used to create directories recursively. The makedirs() function The basic syntax format is as follows:

os.makedirs(name,mode=0o77)

Among them, name means to specify the directory to be created, and can use an absolute path or a relative path, mode means to specify a numerical mode, and the default value is 0777.

2.5 Delete directory

Deleting a directory can be achieved by using the rmdir() function provided by the os module. Deleting a directory through the rmdir() function only works when the directory to be deleted is empty. The basic syntax of the rmdir() function is as follows:

os.rmdir(path)

Wherein, path is the directory to be deleted, and can use a relative path or an absolute path. For example, to delete the directory in the demo file in the C drive, the code that can be used is .

import os
os.rmdir("C:\\demo\\mr")
2.6 Traversing directories

In ancient Chinese, traversal means to go all over and travel around. In Python, the meaning of traversal is similar to this. It is to run all directories (including subdirectories) and files under the specified directory. In Python, The walk() function of the os module is used to realize the function of traversing directories. The basic syntax of the walk() function is as follows.

os.walk(top[,topdown][,onerror][,followlinks])

For example: traverse the specified directory.

import os   # 导入os模块
path = "C:\\demo"       # 指定要遍历的根目录
print("【",path,"】 目录下包括的文件和目录:")
for root, dirs, files in os.walk(path, topdown=True):  # 遍历指定目录
    for name in dirs:            # 循环输出遍历到的子目录
        print("∏",os.path.join(root, name))
    for name in files:           # 循环输出遍历到的文件
        print("≌",os.path.join(root, name))

3. Advanced file operations

In addition to operating on directories, the built-in Python os module can also perform some advanced operations on files, for example:
3.1 Delete files
Python does not have a built-in function to delete files, but the built-in os module provides a function to delete files remove (), the basic syntax of this function is as follows:

os.remove(path)

Among them, path is the path of the file to be deleted. You can use a relative path or an absolute path.
3.2 Renaming files and directories
The os module provides the function rename() for renaming files and directories. If the specified path is a file, then Rename the file, if the specified path is a directory, then rename the directory, the basic syntax format is:

os.rename(src,dst)

3.3 Get the basic information of the file
After the file is created on the computer, the file itself will contain some information, for example, basic information such as the time of the last access to the file, the modification time of the last access, the file size, etc., can be obtained through the os module The stat() function can obtain the basic information of these files. The basic syntax of the stat() function is as follows:

os.stat(path)

Among them, path represents the file path to obtain the basic information of the file, which can be a relative path or an absolute path.
For example: Get basic text information

import os   # 导入os模块
fileinfo = os.stat("mr.png") # 获取文件的基本信息
print("文件完整路径:", os.path.abspath("mr.png")) # 获取文件的完整数路径
# 输出文件的基本信息
print("索引号:",fileinfo.st_ino)
print("设备名:",fileinfo.st_dev)
print("文件大小:",fileinfo.st_size," 字节")
print("最后一次访问时间:",fileinfo.st_atime)
print("最后一次修改时间:",fileinfo.st_mtime)
print("最后一次状态变化时间:",fileinfo.st_ctime)

The running results are as follows:
insert image description here
Since the time and the number of bytes displayed in the above results are a long series of integers, which are different from what we usually see, the values ​​will be formatted, and the code is as follows.

import os   # 导入os模块

def formatTime(longtime):
    '''格式化日期时间的函数
       longtime:要格式化的时间
    '''
    import time  # 导入时间模块
    return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(longtime))
def formatByte(number):
    '''格式化日期时间的函数
       number:要格式化的字节数
    '''    
    for (scale,label) in [(1024*1024*1024,"GB"),(1024*1024,"MB"),(1024,"KB")]:
        if number>= scale:   # 如果文件大小大于等于1KB
            return "%.2f %s" %(number*1.0/scale,label)
        elif number == 1: # 如果文件大小为1字节
            return "1 字节"
        else:   # 处理小于1KB的情况
            byte = "%.2f" % (number or 0) 
    return (byte[:-3] if byte.endswith('.00') else byte)+" 字节"  # 去掉结尾的.00,并且加上单位“字节”


if __name__ == '__main__':
    fileinfo = os.stat("mr.png") # 获取文件的基本信息
    print("文件完整路径:", os.path.abspath("mr.png")) # 获取文件的完整数路径
    # 输出文件的基本信息
    print("索引号:",fileinfo.st_ino)
    print("设备名:",fileinfo.st_dev)
    print("文件大小:",formatByte(fileinfo.st_size))
    print("最后一次访问时间:",formatTime(fileinfo.st_atime))
    print("最后一次修改时间:",formatTime(fileinfo.st_mtime))
    print("最后一次状态变化时间:",formatTime(fileinfo.st_ctime))

The result of the operation is as follows:

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_43401243/article/details/95066042