Home  >  Article  >  Backend Development  >  How to use the fileinput module for file iteration in Python 3.x

How to use the fileinput module for file iteration in Python 3.x

WBOY
WBOYOriginal
2023-07-29 23:45:411222browse

How to use the fileinput module for file iteration in Python 3.x

In Python programming, we often need to operate files, such as reading file contents, writing file contents, etc. When processing multiple files, the fileinput module can be used to easily perform file iteration operations. This article will introduce how to use the fileinput module for file iteration in Python 3.x, and provide code examples.

The fileinput module is part of the Python standard library. It provides a convenient way to iterate through the contents of multiple files and can handle file read and write operations during the same file iteration process.

The basic steps for file iteration using the fileinput module are as follows:

  1. Import the fileinput module:

    import fileinput
  2. Create the fileinput object:

    files = fileinput.input(files=['file1.txt', 'file2.txt', ...])

    Here a list of file names is passed in as a parameter, indicating the files to be iterated. If no list of filenames is specified, standard input is iterated by default.

  3. Use a for loop for file iteration:

    for line in files:
        # 对每一行内容进行处理
        print(line)
  4. Close the fileinput object:

    files.close()

Below It is a complete example that demonstrates the use of the fileinput module:

import fileinput

# 创建fileinput对象,迭代文件列表file1.txt和file2.txt的内容
with fileinput.input(files=['file1.txt', 'file2.txt']) as files:
    # 对文件内容进行迭代处理
    for line in files:
        # 打印每一行内容
        print(line)

    # 在文件末尾写入一行内容
    with open('file1.txt', 'a') as f:
        f.write('Appended line')

# 关闭fileinput对象
files.close()

In the above code, we first imported the fileinput module, and then used the with statement to create a fileinput object. In the with statement block, we use a for loop to iterate over the file contents and print the contents of each line. Next, we use the open function to open the file1.txt file in append mode and append a line to the end of the file. Finally, we close the fileinput object.

Using the fileinput module for file iteration operations can simplify the processing of multiple files. Whether it is reading file content, writing file content, or other file operations, the fileinput module provides a simple and convenient way. I hope this article can help you better understand and use the fileinput module.

The above is the detailed content of How to use the fileinput module for file iteration in Python 3.x. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn