SoFunction
Updated on 2025-04-14

Extra detailed explanation of the basic Python file operation methods (detailed version)

1. File operation

1. File opening and closing

1.1 Open the file

In Python, you can useopen()Function to open the file.

Here is a simple example:

# Open the file (default is read-only mode)file_path = ''
with open(file_path, 'r') as file:
    # Perform file operations, such as reading file contents    file_content = ()
    print(file_content)

# The file will be automatically closed after the with block is finished, without explicitly closing the file

In the above example:

  • ''It is the path and name of the file. You can modify it to the file you want to open according to the actual situation.

  • 'r'Represents read-only mode. If you want to write to a file, you can use'w'Mode, if you want to add content, you can use'a'Mode, etc.

  • with open(...) as file: It is a way to use the context manager to ensure that the file is closed correctly after use, and it can be guaranteed to close even if an exception occurs when processing the file.

1.2 Close the file

There are two main ways to close a file in Python:

1.2.1 Using the with statement

withA statement is a context manager that automatically closes the file when its code block is executed. This is the recommended way because it ensures that the file is closed correctly after use and can be closed even if an exception occurs.

file_path = ''
with open(file_path, 'r') as file:
    # Perform file operations, such as reading file contents    file_content = ()
    print(file_content)
    # The file has been automatically closed here

1.2.2 Use the close() method:

You can explicitly call the file objectclose()Method to close the file. This method is suitable for some special circumstances, but is relatively inferior towithThe sentences are concise and safe.

file_path = ''
file = open(file_path, 'r')
try:
    # Perform file operations, such as reading file contents    file_content = ()
    print(file_content)
finally:
    ()

In usewithNo explicit call is required when a statementclose()method. If you open the file in the code without using itwith, please make sure to call it in the appropriate placeclose()to close the file to avoid resource leakage.

2. Access mode and instructions

Access Mode illustrate
r Open the file read-only. The pointer to the file will be placed at the beginning of the file. This is the default mode.
w Open a file for writing only. If the file already exists, overwrite it. 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. That is to say, new content will be written after existing content. If the file does not exist, create a new file for writing.
rb Open a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode.
wb Opening a file in binary format is only for writing. If the file already exists, overwrite it. If the file does not exist, create a new file.
ab Open a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. That is to say, new content will be written after existing content. If the file does not exist, create a new file for writing.
r+ Open a file for reading and writing. The file pointer will be placed at the beginning of the file.
w+ Open a file for reading and writing. If the file already exists, overwrite it. If the file does not exist, create a new file.
a+ Open a file for reading and writing, and if the file already exists, the file pointer will be placed at the end of the file. The file will be in append mode when it is opened. If the modified file does not exist, create a new file for reading and writing.
rb+ Open a file in binary format for reading and writing. The file pointer will be placed at the beginning of the file
wb+ Open a file in binary format for reading and writing. If the modified file already exists, it will be overwritten. If the modified file does not exist, create a new file.
ab+ Open a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the modified file does not exist, create a new file for reading and writing.

2. Read and write files

1. Write data

Writing data often involves saving information into a file, database, or other persistent storage medium. Here are some common data writing scenarios:

1.1 Write text files

Use built-inopenFunction to open the file and write the content. Make sure to use the appropriate mode (e.g.'w'represents writing).

file_path = ''

# Write to a filewith open(file_path, 'w') as file:
    ("Hello, this is some data.")

1.2 Write to CSV file

usecsvModule to write files in CSV format.

import csv

csv_file_path = ''

data = [['Name', 'Age', 'Occupation'],
        ['John Doe', 30, 'Engineer'],
        ['Jane Smith', 25, 'Designer']]

with open(csv_file_path, 'w', newline='') as csvfile:
    csv_writer = (csvfile)
    csv_writer.writerows(data)  

1.3 Write to JSON file

Use built-injsonModule to write to JSON format files.

import json

json_file_path = ''

data = {"name": "John Doe", "age": 30, "occupation": "Engineer"}

with open(json_file_path, 'w') as jsonfile:
    (data, jsonfile)

1.4 Write to the database

Use database connection library (e.g.sqlite3、mysql-connector-pythonetc.) Interact with the corresponding database.

import sqlite3

# Connect to SQLite database (assuming there is a database named )conn = ('')

# Create a cursor objectcursor = ()

# Execute SQL insert statement("INSERT INTO users (name, age, occupation) VALUES (?, ?, ?)", ('John Doe', 30, 'Engineer'))

# Submit changes()

# Close the connection()

2. Read data (read)

Reading data usually involves retrieving information from a file, database, or other storage medium. Here are some common examples of reading data:

2.1 Reading text files

Use built-inopenFunction to open the file and read the content.

file_path = ''

# Read the filewith open(file_path, 'r') as file:
    data = ()
    print(data)

2.2 Reading CSV files

usecsvModule to read CSV format files.

import csv

csv_file_path = ''

# Read CSV filewith open(csv_file_path, 'r') as csvfile:
    csv_reader = (csvfile)
    for row in csv_reader:
        print(row)

2.3 Reading JSON files

Use built-injsonModule to read files in JSON format.

import json

json_file_path = ''

# Read JSON filewith open(json_file_path, 'r') as jsonfile:
    data = (jsonfile)
    print(data)

2.4 Reading data from the database

Use database connection library (e.g.sqlite3、mysql-connector-pythonetc.) Interact with the corresponding database.

import sqlite3

# Connect to SQLite database (assuming there is a database named )conn = ('')

# Create a cursor objectcursor = ()

# Execute SQL query statement("SELECT * FROM users")

# Search all rowsrows = ()

# Print each linefor row in rows:
    print(row)

# Close the connection()

2. Readlines

readlinesis one of the methods used in Python to read files. It is used to read file content line by line and store each line as a string in a list. The following is the rightreadlinesDetailed explanation of the method:

Basic syntax for using readlines method

with open('', 'r') as file:
    lines = ()

explain:

  • open('', 'r'): Open the file''For reading. The first parameter is the file name, and the second parameter is the mode of opening the file.'r'Represents read-only mode.

  • with ... as ...: usewithThe statement can ensure that the file is automatically closed after reading is completed, and there is no need to explicitly call it.()

  • lines = () : readlinesMethods are used to read all lines of a file and store each line as a string in the list.linesmiddle.

  • Each list element corresponds to a line of text in the file. You can use list indexes to access specific rows, e.g.lines[0]Represents the first line of the file.

Example: Assume that ‘’ contains the following:

Hello, this is line 1.
This is line 2.   
And this is line 3.   

After using readlines:

with open('', 'r') as file:
    lines = ()

# lines is now a list containing each line of textprint(lines)
#Output:# ['Hello, this is line 1.\n', 'This is line 2.\n', 'And this is line 3.\n']

# Access specific rowsprint(lines[0].strip())  # Output: Hello, this is line 1.

Notes:

  • The end of each line contains a newline character\n, you can usestrip()Method to remove these extra whitespace characters.

  • readlinesThe method is suitable for handling files containing multiple lines of text, but for large files, you may want to consider reading line by line rather than loading the entire file into memory. This can be achieved by looping through the file object instead of usingreadlines

3. Readline

readline is one of the methods used in Python to read files. It is used to read file contents line by line and return a line in the file as a string. The following is a detailed explanation of the readline method:
Basic syntax for using readline method

with open('', 'r') as file:
    line = ()

explain:

  • open('', 'r'): Open the file''For reading. The first parameter is the file name, and the second parameter is the mode of opening the file.'r'Represents read-only mode.

  • with ... as ...: usewithThe statement can ensure that the file is automatically closed after reading is completed, and there is no need to explicitly call it.()

  • line = () : readlineMethod is used to read a line of a file and store the line as a string in a variablelinemiddle.

Example: Assume that ‘’ contains the following:

Hello, this is line 1.   
This is line 2.   
And this is line 3.   

After using readline:

with open('', 'r') as file:
    line1 = ()
    line2 = ()
    line3 = ()

print(line1)  # Output: Hello, this is line 1.print(line2)  # Output: This is line 2.print(line3)  # Output: And this is line 3.

Notes:

  • EachreadlineThe call will read the next line of the file.

  • The returned string contains the line break at the end of the line.\n. If you do not need a newline, you can usestrip()Method to remove it.

  • When the file is read,readlineThe empty string ‘’ will be returned, so it can be used in the loopwhile line != ''To read the entire file line by line.

Looping to read the entire file:

with open('', 'r') as file:
    line = ()
    while line != '':
        print(())  # Remove newlines        line = ()

This loop will read the entire file line by line until the end of the file.

4. The difference between readlines and readline

readlinesandreadlineThere are two different methods used in Python to read files, and there are some important differences between them:

4.1 readlines method:

  • Return type:readlinesThe method returns a list containing all lines of the file, where each element is a line of text string in the file.

  • Usage: Suitable for processing files containing multiple lines of text, the entire file can be loaded into memory at one time. This method is suitable for situations where files are small and can be fully loaded into memory.

  • example:

with open('', 'r') as file:
    lines = ()`

4.2 readline method:

  • Return type:readlineEach call of the method returns only one line in the file as a string. If called again, the next line will be returned. When the file is read, the empty string ‘’ is returned.

  • Usage: Suitable for processing large files line by line, it can effectively reduce memory usage. Because it reads only one line at a time, it can process the file line by line in a loop without having to load the entire file into memory.

  • example:

  with open('', 'r') as file:
      line = ()
      while line != '':
          print(())  # Remove newlines          line = ()

4.3 Summary of differences:

  • readlinesRead all lines of the entire file at once and return a list containing all lines.

  • readlineRead the file line by line, and return one line in the file each call. It is suitable for processing large files and reducing memory usage.

  • readlinesReturns each line containing a newline, andreadlineReturning a separate line, the newline needs to be removed manually.

Choosing which method to use depends on the file size and processing requirements. If the file is small, it can be fully loaded into memory and usereadlines;If the file is large, it can be processed line by line and usereadline

3. Document related operations

1. File renaming

Python file renaming is a basic operation in file management and can be implemented through Python's built-in libraries. Here is a very detailed starter guide on how to rename files using Python:

1.2 Import the necessary libraries

First, you need to import Python'soslibrary, which provides many functions that interact with the operating system.

import os

1.2 Prepare the file list

To rename a file, you need to list all files in the specified directory first. Can be used()Function to get the list of files in the directory.

# List all files and folders in the specified directoryfiles = ('path_to_directory')

1.3 Traversing the file list

Next, you need to iterate through the file list and rename each file.

for file in files:
    # Get the full path to the file    full_path = ('path_to_directory', file)
    
    # Check if it is a file    if (full_path):
        # New file name        new_filename = 'new_name'
        
        # Rename operation        (full_path, ('path_to_directory', new_filename))
        print(f'Renamed {file} to {new_filename}')

1.4 Exception handling

When renaming a file, various exceptions may occur, such as the target file already exists, insufficient permissions, etc. To ensure the robustness of the program, exception handling should be added.

try:
    for file in files:
        # ... (code above)except OSError as e:
    print(f'Error occurred: {e}')

1.5 Complete script example

import os
# Specify the directory to rename the filedirectory = 'path_to_directory'
# List all files in the directoryfiles = (directory)
# traverse the file list and rename itfor file in files:
    if ((directory, file)):
        # Set a new file name        new_filename = 'new_name'
        
        # Rename the file        try:
            (
                (directory, file),
                (directory, new_filename)
            )
            print(f'Renamed {file} to {new_filename}')
        except OSError as e:
            print(f'Error renaming {file}: {e}')

1.6 Pay attention to safety and effectivenessRate

When renaming files in batches, make sure:

  • Do not perform multiple rename operations at the same time to avoid potential race conditions.

  • Make sure the target directory exists and avoid creating non-existent directories when renaming.

  • Considering the operating system's limitation on file renaming, for example, in Windows, the file name cannot exceed 255 characters, but in Unix/Linux there is no such limit.

1.7 Advanced Usage

For more complex renaming tasks, you can use regular expressions or other text processing methods to generate new file names.

import os
import re
# Specify the directorydirectory = 'path_to_directory'
# List all files in the directoryfiles = (directory)
# traverse the file list and rename itfor file in files:
    if ((directory, file)):
        # Use regular expressions to match file name patterns and replace them with new patterns        new_filename = (r'\d+', 'new_prefix', file)
        
        # Rename the file        try:
            (
                (directory, file),
                (directory, new_filename)
            )
            print(f'Renamed {file} to {new_filename}')
        except OSError as e:
            print(f'Error renaming {file}: {e}')

This script renames all files in the specified directory that start with numbers to a new prefix.

2. Delete the file

In Python, deleting files is a relatively simple operation. We can useosIn the library()Functions to implement. Here is a very detailed guide to how to delete files using Python:

2.1 Import the necessary libraries

First, you need to import Python'soslibrary, which provides many functions that interact with the operating system.

import os

2.2 Prepare file path

To delete a file, you need to know the path to the file you want to delete.

file_path = 'path_to_file'

2.3 Check whether the file exists

Before deleting a file, it is best to check if the file exists to avoid errors.

if (file_path):
    print(f'File {file_path} exists, proceed to delete.')
else:
    print(f'File {file_path} does not exist, skip deletion.')

**2.4 Perform the delete operation

If the file exists, you can use()Function to delete it.

try:
    (file_path)
    print(f'File {file_path} deleted successfully.')
except OSError as e:
    print(f'Error occurred: {e}')

2.5 Complete script example

import os
# Specify the directory of the file to be deletedfile_path = 'path_to_file'
# Check if the file existsif (file_path):
    print(f'File {file_path} exists, proceed to delete.')
else:
    print(f'File {file_path} does not exist, skip deletion.')
# Perform a delete operationtry:
    (file_path)
    print(f'File {file_path} deleted successfully.')
except OSError as e:
    print(f'Error occurred: {e}') 

2.6 Pay attention to safety and efficiency

When deleting files in batches, make sure:

  • Do not perform multiple deletion operations at the same time to avoid potential race conditions.

  • Make sure the target directory exists and avoid creating non-existent directories when deleting.

  • Considering the operating system's operational restrictions on file deletion, for example, in Windows, the file name cannot exceed 255 characters, but in Unix/Linux there is no such limitation.

Through the above steps, you should be able to master how to delete files using Python.

3. Create a file

In Python, creating files is a relatively simple operation. We can useos In the library ()Function orwithStatement to create a file. Here is a very detailed starter guide on how to create files using Python:

3.1 Import the necessary libraries

First, you need to import Python'soslibrary, which provides many functions that interact with the operating system.

import os   

3.2 Prepare file path

To create a file, you need to know the path to the file you want to create.

file_path = 'path_to_file'   

3.3 Check whether the file path exists

Before creating a file, it is best to check if the file path exists to avoid overwriting other files.

if not (file_path):
    print(f'File path {file_path} does not exist, proceed to create.')
else:
    print(f'File path {file_path} already exists, skip creation.')  

3.4 Perform the creation operation

If the file path does not exist, you can use()Functions to create files.

try:
    with open(file_path, 'w') as f:
        print(f'File {file_path} created successfully.')
except IOError as e:
    print(f'Error occurred: {e}')

Here, we usewithStatement to ensure that the file will be closed correctly after the operation is completed.'w'Parameters indicate that the file is opened in write mode. If the file does not exist, a new file will be created.

3.5 Complete script example

import os
# Specify the directory of the file to be createdfile_path = 'path_to_file'
# Check whether the file path existsif not (file_path):
    print(f'File path {file_path} does not exist, proceed to create.')
else:
    print(f'File path {file_path} already exists, skip creation.')
# Perform a creation operationtry:
    with open(file_path, 'w') as f:
        print(f'File {file_path} created successfully.')
except IOError as e:
    print(f'Error occurred: {e}')

3.6 Pay attention to safety and efficiency

When creating a file, make sure:

  • Have sufficient permissions to create the file.

  • Avoid creating large files when there is insufficient memory.
    Through the above steps, you should be able to master how to create files in Python.

4. Get the current directory

In Python, we can useos In the library ()Function to get the path to the current directory. Here is an example:

import os
current_directory = ()
print(f'Current directory is: {current_directory}')

This will print out the path to the directory where the current Python script is located.

IV. Example

1. Automatic cleaning of directory .txt

1.1 You need to empty 4 grids at the front of the line where the second-level title is located, and you don’t need the first-level title.

1.2 You need to add a space after the chapter and chapter words

1.3 You need to add an => symbol before the page number

# Get the desktop pathimport os
import re

desktop_path = (("~"), "Desktop")

# target file pathfile_path = (desktop_path, "Directory.txt")

# Open the file and read the contentwith open(file_path, 'r', encoding='utf-8') as file:
    lines = ()

modified_lines = []
for line in lines:
    # Remove spaces    line = (" ", "")
    if len(line) == 1:
        continue
    # Use regular expression to add a space after the 'chapter' or 'section', only if there are no spaces after it    line = (r'(chapter|Festival)(?![ ])', r'\1 ', line)
    # Add space after the decimal point    line = (r'(\.\d)', r'\1 ', line)
    if 'chapter' not in line:
        # Add 4 spaces to the secondary title        line = ' ' * 4 + line
    # Match and remove the outermost English brackets    pattern_en = r'\(([\d\s]+)\)'
    line = (pattern_en, r'\1', line)
    # Match and remove the outermost Chinese brackets and their internal contents (including characters other than numbers and spaces)    pattern = r'(([^)]+))'
    line = (pattern, r'\1', line)
    # Make sure there is only one per line =>    if '=>' not in line:
        # Add => before page number (only at the end of the line)        line = (r'(\d+)$', r'=>\1', line)
    # Remove Chinese characters and '=> redundant symbols on the left side of the overall symbol    pattern = r'([\u4e00-\u9fff]+)[^\w\s]+=>'
    line = (pattern, r'\1=>', line)
    modified_lines.append(line)
# Write the modified content back to the filewith open(file_path, 'w', encoding='utf-8') as file:
    (modified_lines)

# Read file contentwith open(file_path, 'r', encoding='utf-8') as file:
    content = ()
    print(content)

2. Bulkly modify the file naming in the folder

You can usePythonofosThe module implements batch modification of file names and combines string operations to ensure the specified format in the file names. Here is a sample code:

import os

# Specify directory pathdirectory_path = r'Absolute path to the target folder'

# Get all file names in the directoryfiles = (directory_path)

# traverse filesfor file_name in files:
    # Build a complete file path    file_path = (directory_path, file_name)

    # Check if the file is an image file and the file name contains an underscore    if file_name.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')) and '_' in file_name:
        # Split file name, underlined        parts = file_name.split('_')

        # Make sure the first part after the split is '00159231127'        if parts[0] != '00159231127':
            # Build a new file name            new_file_name = '00159231127' + '_' + '_'.join(parts[1:])

            # Build a new file path            new_file_path = (directory_path, new_file_name)

            # Rename the file            (file_path, new_file_path)
            print(f'Renamed: {file_name} -> {new_file_name}')

In the above code:

1. UseGet all file names in the directory and then iterate through these file names.
2. PassBuild a complete file path to ensure the correctness of the path.
3. Check whether the file is a picture file (to.png, .jpg, .jpeg, .gifending file), and the file name contains an underscore.
4. Usesplit('_')Segment the file name to ensure that the first part after division is'00159231127'
5. Build a new file name and use . to rename the file.

Before running, make sure to back up the files or run them in a test environment to avoid accidentally corruption of the files.

3. Check whether the folder with the same name exists in the same directory

Check whether the first 5 digits of the folder name under the target path exist, and print them out if they are the same.

You can usePythonTo check the folders in the specified directory and find the folders named in the first 5 digits. Here is a sample code:

import os

# Specify directory pathdirectory_path = r'Target Path'

# Get all folder names in the directoryfolders = [folder for folder in (directory_path) if ((directory_path, folder))]

# Create an empty dictionary to store the first 5 identical folder namessame_prefix_folders = {}

# traverse foldersfor folder in folders:
    # Get the first 5 folder names    prefix = folder[:5]

    # Check whether the first 5 folder names are already in the dictionary    if prefix in same_prefix_folders:
        # Add the folder name to the corresponding key value        same_prefix_folders[prefix].append(folder)
    else:
        # If the first 5 folder names are not in the dictionary, create a key-value pair        same_prefix_folders[prefix] = [folder]

# Output the same folder name as the first 5 digitsfor prefix, folders in same_prefix_folders.items():
    if len(folders) > 1:
        print(f"forward5Position is '{prefix}' The folder has the following duplicate name:")
        print(', '.join(folders)) 

This code does the following:

1. UseGets all folder names in the specified directory.

2. Then iterate through these folder names, extract the first 5 names, and place the folder with the same prefix into a dictionary.

3. Finally print out the folder name of the first 5 digits.

Summarize

This is the article about this detailed explanation of the operation methods of Python basic files. For more related contents of Python basic files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!