Master Python file operations: all-round exploration from basic to advanced

In this blog, we will discuss file operations in Python comprehensively and in-depth. File operations are an integral part of Python programming, which includes various operations such as opening, reading, writing, and closing files. We will explain from basic file operations to advanced file processing skills, and how to use Python for file operations elegantly. In each part, we will share some unique usages, with specific sample code and output results. At the end of the article, we will also share some little-known but very useful file manipulation tips.

B station project actual combat video: Python automated testing: 60 actual combat projects, teach you step by step

Basic file operations: opening, reading, writing, and closing files

Python uses a built-in open()function to open a file, which returns a file object, and the common modes are as follows: 'r' means read, 'w' means write (the original file will be cleared first), 'a' means append,' b' indicates binary mode. In particular, we can use withkeywords so that the file will be automatically closed when we are done.

# 以读取模式打开一个文件
with open('file.txt', 'r') as file:
    content = file.read()
print(content)

When you run this code, hypothetically file.txt, Hello, Python!you'll see the following output:

Hello, Python!

File reading: fine-grained control

Instead of reading the entire file at once, we can also read the file in smaller units. This is very useful when dealing with large files to effectively control memory usage.

with open('file.txt', 'r') as file:
    while True:
        line = file.readline()
        if not line:
            break
        print(line, end='')

This code reads file.txtthe contents of the file line by line and prints them. where , end=''is to prevent printthe function from inserting a newline after each print, since every line read from the file already has a newline.

File writing: content appending and overwriting

We use write mode ('w') or append mode ('a') to write to the file. \nHere's a little trick: If your program needs to write to the file frequently, it is more efficient to use a newline when writing than to use write()a function and then use write('\n')a function to insert a newline.

with open('file.txt', 'a') as file:
    file.write('Hello, Python!\n')

This code will file.txtappend a line to the end of the file Hello, Python!.

More advanced file handling: osand shutilmodules

Python osmodules and shutilmodules provide us with more advanced file manipulation functions, such as renaming files, deleting files, creating

directories, copy files, etc.

import os
import shutil

# 创建一个新的目录
os.mkdir('new_folder')

# 重命名文件
os.rename('old.txt', 'new.txt')

# 复制文件
shutil.copy2('src.txt', 'dst.txt')

# 删除文件
os.remove('file_to_delete.txt')

Running this code will perform the above file operations in sequence.

File encoding: handle files with different encoding formats

When dealing with files, we may encounter various encoding formats, such as UTF-8, ASCII, ISO-8859-1, etc. Python's open()function allows us encodingto specify the encoding method of the file through parameters.

with open('file.txt', 'r', encoding='utf-8') as file:
    content = file.read()
print(content)

If you try to read a text file that contains non-ASCII characters, but do not provide the correct encoding parameter, Python may throw UnicodeDecodeError. In this case, you need to know the correct encoding format of the file in order to read the file correctly.

# 尝试读取一个包含非ASCII字符的文件,但没有指定正确的编码格式
try:
    with open('file.txt', 'r') as file:
        content = file.read()
except UnicodeDecodeError:
    print("UnicodeDecodeError occurred!")

This code catches UnicodeDecodeErrorand prints an error message.

File Exception Handling: Ensuring Code Robustness

In the file operation, we may encounter various abnormal conditions, such as the file does not exist, there is no read/write permission, etc. We can use Python's exception handling mechanism to catch these exceptions and deal with them accordingly.

try:
    with open('nonexistent_file.txt', 'r') as file:
        content = file.read()
except FileNotFoundError:
    print('File does not exist!')
except PermissionError:
    print('No permission to read the file!')

This code captures FileNotFoundErrorand PermissionError, and prints out the corresponding error message, respectively.

pickleObject serialization and deserialization using

Python's picklemodules provide functions for converting objects into a format that can be stored in a file or transmitted over a network (a process called serialization), and reconstructing objects from this format (a process called deserialization) . This is a very convenient way to save and load Python objects.

import pickle

data = {
    'name': 'John',
    'age': 30,
    'pets': ['cat', 'dog']
}

# 序列化并保存到文件
with open('data.pkl', 'wb') as file:
    pickle.dump(data, file)

# 从文件加载并反序列化
with open('data.pkl', 'rb') as file:
    loaded_data = pickle.load(file)

print(loaded_data)

Run this code, and you'll see the following output:

{'name': 'John', 'age': 30, 'pets': ['cat', 'dog']}

This is the original datadictionary object.

file path handling

When dealing with file paths, Python's os.pathmodules provide a series of functions to parse, construct, and modify file paths. These functions are cross-platform, so you can safely use them whether your program runs on Windows, macOS, or Linux.

import os

# 获取文件的绝对路径


abs_path = os.path.abspath('file.txt')
print(f'Absolute path: {abs_path}')

# 获取文件所在的目录
dir_name = os.path.dirname(abs_path)
print(f'Directory: {dir_name}')

# 获取文件的基本名和扩展名
base_name = os.path.basename(abs_path)
print(f'Base name: {base_name}')
root, ext = os.path.splitext(base_name)
print(f'Root: {root}, Extension: {ext}')

Assuming it file.txtis /home/user/documents/in a directory, running this code will output:

Absolute path: /home/user/documents/file.txt
Directory: /home/user/documents
Base name: file.txt
Root: file, Extension: .txt

One More Thing: File Traversal and Search

Python's osmodule provides a os.walk()function, which is an easy-to-use yet powerful tool for generating filenames in directory trees. Combined with fnmatchmodules, we can implement pattern matching searches for files.

import os
import fnmatch

def find(pattern, path):
    result = []
    for root, dirs, files in os.walk(path):
        for name in fnmatch.filter(files, pattern):
            result.append(os.path.join(root, name))
    return result

# 查找当前目录及其所有子目录中的所有.txt文件
print(find('*.txt', '.'))

file1.txtAssuming that there are three files ,  file2.txtin the current directory and its subdirectories sub/file3.txt, running this code will output:

['./file1.txt', './file2.txt', './sub/file3.txt']

We hope that you can learn various skills of Python file manipulation from this blog, and can play a role in your Python programming journey. If you have any questions or ideas, please leave a message to communicate.


Thanks to everyone who read my article carefully, although it is not a very valuable thing, if you can use it, you can take it away:

 These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you too!

Information acquisition method:

Guess you like

Origin blog.csdn.net/qq_56271699/article/details/131207859