10 Python file operations

overview

        In the previous section, we introduced Python derivations, including: list derivation, tuple derivation, set derivation, dictionary derivation, conditional derivation, etc. In this section, we will introduce Python's file operations. Python is a high-level programming language that provides many built-in functions and modules to handle file operations, mainly including: opening files, reading files, writing files, closing files, getting directory listings, etc.

open a file

        In Python, you can use the open() function to open a file. The function usually accepts two parameters: file path and open mode.

        Common modes for opening files are as follows:

        'r' : read-only mode.

        'w' : write mode. If the file exists, it is truncated to an empty file; if the file does not exist, a new file is created.

        'a' : append mode. If the file exists, append the new data to the end of the file; if the file does not exist, create a new file.

        'x' : Create mode. Create a new file, failing if the file already exists.

        't' : text mode, default when no mode is specified.

        'b' : binary mode. Using an open mode marked with 'b' (eg: 'rb', 'wb'), files can be read and written in binary mode, which is useful when working with images, video, audio and other non-text files.

# 打开名为text.txt的文件以进行读取
file = open('text.txt', 'r')

read file

        After opening a file, you can use the read() function to read the contents of the file. The function accepts an optional parameter representing the maximum number of characters to read. Alternatively, you can use the readline() function to read one line at a time, and the readlines() function to read all lines at once and return a list.

# 打开名为text.txt的文件以进行读取
file = open('text.txt', 'r')
# 读取整个文件内容  
content = file.read()
# 最多读取100个字符  
content = file.read(100)
# 读取一行内容
content = file.readline()
# 读取所有行内容,并返回一个列表
content = file.readlines()
# 使用完需要关闭文件
file.close()

write to file

        To write content to a file, use the write() function. The function accepts a string parameter representing the content to be written. You can also use the writelines() function, which accepts a sequence of strings, but does not automatically add line breaks and needs to be added manually.

# 打开名为text.txt的文件以进行读取
file = open('text.txt', 'w')
# 写入字符串内容
file.write('hello CSDN\n')
# 写入列表,writelines不会自动添加换行符,需要手动添加
lines = ['first line', 'second line', 'third line']
file.writelines('\n'.join(lines))
file.close()

close file

        After using the file, remember to use the close() function to close the file. This is a good programming practice because it frees up system resources and avoids exceptions or errors.

        In fact, the above sample codes are not perfect, because the open() function will throw an OSError exception when it fails. For a more standardized way of writing, please refer to the sample code below.

file = None
try:
    file = open('text.txt', 'r')
    print(file.readline())
except OSError as error:
    print(error)
finally:
    if file:
        file.close()

        But it is too cumbersome to encode like this every time you read and write files. Therefore, Python provides the with statement. The with statement is used with file objects to ensure that the file is properly closed after use. This is the recommended way of handling files, as it automatically manages the opening and closing of files, thereby avoiding resource leaks.

        When you use the with statement to process a file, Python automatically closes the file at the end of the code block, regardless of whether an exception occurred in the middle. This makes for cleaner and safer code, since you don't have to worry about forgetting to close the file.

try:
    with open('text.txt', 'r') as file:
        content = file.read()
        print(content)
except:
    print('open exception')

        In the above sample code, we use the open() function to open a file named 'text.txt' and assign it to the variable file. Then, wrap the file with a with statement. In the with block, we read the content of the file and store it in the variable content and then print it. Python automatically closes the file when the code block ends, regardless of whether an exception occurred in the middle.

        In addition, you can also use the with statement to handle other types of resources, such as: network connections, thread locks, etc. This way of automatically managing resources can ensure that resources are properly released after use, thereby avoiding problems such as resource leaks and program crashes.

file pointer movement

        To move the file pointer to a specified location, use the seek() function. The file pointer is an internal variable that indicates where in the file the current read and write position is. The seek() function accepts two parameters: the first parameter is used to specify the new position, in bytes; the second parameter is optional, indicating the position to start offsetting from, and 0 means counting from the beginning of the file (default value), 1 means counting from the current position, 2 means counting from the end of the file. After moving, the next read and write operation will start from the specified position.

file = open('text.txt', 'r')
# 读取前6个字符
data = file.read(6)
# 移动文件指针到文件开头
file.seek(0)
# 再次读取前6个字符
data = file.read(6)
file.close()

        In addition to the seek() function, you can also use the tell() function to obtain the position of the current file pointer. The tell() function returns an integer representing the position of the current file pointer.

file = open('text.txt', 'r')
data = file.read(6)
position = file.tell()
print(position)
file.close()

get directory listing

        In Python, a directory listing can be obtained using the listdir() function of the os module, which returns a list of the names of all files and directories in a given directory.

import os

cur_dir = os.getcwd()
files = os.listdir(cur_dir)
for file in files:
    print(file)

        In Python 3.4 and later, the contents of directories can also be scanned using the scandir() function of the os module. The scandir() function returns a generator, each iteration produces a ScandirItem object, which represents an entry in the directory, including: files, directories, links, etc.

import os

cur_dir = os.getcwd()
for item in os.scandir(cur_dir):  
    print(item.path, item.is_file())

        We can also use the Path() function of the pathlib module to return a PosixPath or WindowsPath object, which has an iterdir() function to create an iterator over all the files and directories under that directory. Each entry generated by the iterdir() function contains information about the file or directory, such as: name, file attributes, etc.

import os
from pathlib import Path

cur_dir = os.getcwd()
entries = Path(cur_dir)
for entry in entries.iterdir():
    print(entry.name, entry.is_dir())

        In addition to the iterdir() function, the Path object also includes many useful functions, see the introduction below.

        Path.mkdir(mode=0o777, parents=False, exist_ok=False) : Create a directory. The exist_ok=True parameter will avoid raising an exception if the directory already exists. Note: You need to ensure that the current user has sufficient permissions to create the directory, an exception may be thrown if the directory permissions do not allow creation or other errors occur.

        Path.unlink(missing_ok=False) : delete the file. Note: You need to ensure that the current user has sufficient permissions to delete the file, an exception may be thrown if the file permissions do not allow deletion or other errors occur.

        Path.chmod(mode) : Change file permissions. Note: You need to ensure that the current user has sufficient permissions to change the file permissions. If insufficient permissions or other errors occur, an exception may be thrown.

        Path.chown(uid, gid, recursive=False) : Change the file owner, uid and gid parameters specify the new owner and group. Note: Make sure the current user has sufficient permissions to change the file owner. On some operating systems, this feature may not be available or may be limited. An exception may be thrown if insufficient permissions or other errors occur.

        Path.touch(mode=0o777, exist_ok=True) : Create a new file, if the file already exists, do not change the content. This function requires write permission to create the file, and may throw a permission error if the current user does not have sufficient permissions.

        Path.rmdir(missing_ok=False) : Delete a directory. Note: This function can only delete empty directories. If the directory contains files or other directories, it cannot be deleted. In addition, when using this function, you need to ensure that the current user has sufficient permissions to delete the directory; if the directory permission does not allow deletion or other errors occur, an exception may be thrown.

        Path.rename(new_path) : Rename a file or directory. If the file or directory already exists, it will be overwritten. Note: When using this function, you need to ensure that the current user has sufficient permissions to rename the file or directory; if insufficient permissions or other errors occur, an exception may be thrown.

        Path.exists(path, exist_ok=False) : Check if the path exists.

        Path.is_dir(path) : Check if path is a directory.

        Path.is_file(path) : Check if path is a file.

        Path.glob(pattern) : Perform glob traversal on the given pattern. Note: You need to ensure that the current user has sufficient permissions to access the files in the directory, an exception may be thrown if insufficient permissions or other errors occur.

        Path.resolve(strict=False) : Get the absolute path of a file or directory.

Guess you like

Origin blog.csdn.net/hope_wisdom/article/details/132656167