SoFunction
Updated on 2024-11-12

Summary of Python's implementation of reading files

preamble

Hello guys, today we are going to learn about fileinput.

When it comes to fileinput, probably 90% of coders said they have never used it or even heard of it.

This is not surprising, because in the python world, why bother with fileinput when open can go anywhere?

However, today I'm still going to introduce the fileinput method because it's so Nessie.

It's more than just fragrance. It's real incense!

Next, follow me, with fileinput, yes, that's the FEEL.

1. Introduction to the methodology

basic usage

Let's first look at the basic functionality of fileinput:

(): Returns the name of the file currently being read.

-> Return None before the first line is read.

(): return to the current file as an integer "file descriptor".

-> Returns -1 when the file is not open (between the first line and the file).

(): Returns the cumulative line number that has been read.

-> Returns 0 before the first line is read. returns the line number of the last file after the last line is read.

(): Returns the line number in the current file.

-> Returns 0 before the first line is read.

-> Returns the line number of the last line in this file after the last line of the last file has been read.

Advanced Usage

(): If the line just read is the first line of the file in which it is located, then return True, otherwise return False.

(): Returns True if the last line read was from, False otherwise.

(): closes the current file so that the next iteration will read the first line from the next file (if it exists); lines not read from that file will not be counted in the cumulative line count. The name of the file will not be changed until the first line of the next file has been read.

-> This function will not take effect until the first line has been read; it cannot be used to skip the first file.

-> After the last line of the last file has been read, this function will no longer work.

(): closes the sequence.

2. Default reading

code example

import fileinput

'fileinput defaults to stdin as the input source when the Python script is not passed any arguments'
for line in ():
    print(f'{line}')

running result

Anything you type, the program reads and outputs again.

Common name: repeater

3. Processing a document

code example

import fileinput

'files Just enter the name of the file to open'
with (files=('',)) as file:
    for line in file:
        print(f'{()} (prefix indicating ordinal number, e.g. first, number two etc){()}classifier for objects in rows such as words:{line}',end='')

running result

Parsing:

fileinput has and only has these two read modes: 'r', 'rb'.

() defaults to using mode='r' to read files, if your file is binary, you can use mode='rb'.

4、Processing batch files

Multi-document serial number sequential sorting

invoke a method

(method

code example

import fileinput

'files Just enter the name of the file to open'
with (files=('','')) as file:
    for line in file:
        #() combines two files into a single file object, file, which needs to be sorted for output.
        print(f'{()} (prefix indicating ordinal number, e.g. first, number two etc){()}classifier for objects in rows such as words: {line}', end='')
        
        # () two files read separately, need to be sorted separately
        print(f'{()} (prefix indicating ordinal number, e.g. first, number two etc){()}classifier for objects in rows such as words: {line}', end='')

running result

Separate serial numbers for multiple documents

invoke a method

(method

code example

import fileinput

'files Just enter the name of the file to open'
with (files=('','')) as file:
    for line in file:       
        # () two files read separately, need to be sorted separately
        print(f'{()} (prefix indicating ordinal number, e.g. first, number two etc){()}classifier for objects in rows such as words: {line}', end='')

running result

Usage with glob

In the age of face value, the above output style, can no longer meet our needs, so then, we thought of glob.

code example

import fileinput
import glob

#glob matches txt files that start with te
for line in (("te*.txt")):
    if ():
        # Output read file
        print('='*10,f'Read file{()}','='*10)
        #() method to read the
    print(str(())+ ':'+(),end='')

running result

With that face value, what young lady could not like it.

5. Read and backup

invoke a method

parameter, you can specify the suffix of the backup, e.g. .bak.

code example

import fileinput

# Trigger the action of backup, the content of the source file is modified, and the source file is backed up.
with (files=("",), backup=".bak",inplace=1) as file:
    for line in file:
        print(().replace('111111', '222222'))
        print(f'{()} (prefix indicating ordinal number, e.g. first, number two etc){()}classifier for objects in rows such as words: {line}', end='')

running result

6. Redirection replacement

analyze

In the above example, the inplace parameter is used to indicate whether or not to write the result of the standard output back to the file; it is not replaced by default.

Code Example:

import fileinput

# Trigger the action of backup, the content of the source file is modified, and the source file is backed up.
with (files=("",), inplace=True) as file:
    print("[INFO] task is started...")
    for line in file:
        print(f'{()} (prefix indicating ordinal number, e.g. first, number two etc){()}classifier for objects in rows such as words: {line}', end='')
    print("[INFO] task is closed...")

running result

classifier for sums of money

As you can see by running the results:

The print inside the for loop is now written back to the original file.

print outside the body of the for loop is unchanged.

7. Advancement

Explanation of the meaning of openhook

There is an openhook parameter in (), which supports the user passing in customized object reading methods;

If no hooks are passed, fileinput uses the open function by default;

Methodology

fileinput has two built-in hooks

1、fileinput.hook_compressed(filename, mode)

Use the gzip and bz2 modules to transparently open gzip and bzip2 compressed files (recognized by the extensions '.gz' and '.bz2');

If the file extension is not '.gz' or '.bz2', the file is opened in the normal way (i.e., using open() and without any decompression);

Usage examples: fi = (openhook=fileinput.hook_compressed)

2、fileinput.hook_encoded(encoding, errors=None)

Returns a hook that opens each file via open(), reading the file with the given encoding and errors.

usage example: fi = (openhook=fileinput.hook_encoded(“utf-8”, “surrogateescape”))

sample practice (computing)

Suppose I want to use fileinput to read a file on the network, thoughts:

First use requests to download the file locally

Then use open to read it;

def online_open(url, mode):
    import requests
    r = (url) 
    filename = ("/")[-1]
    with open(filename,'w') as f1:
        (("utf-8"))
    f2 = open(filename,'r')
    return f2

Just pass this function directly to openhook:

# -*- coding:utf-8 -*-
# @Time   : 2022-07-23
# @Author : carl_DJ

import fileinput
file_url = '/'
with (files=(file_url,), openhook=online_open) as file:
    for line in file:
        print(line, end="")

Code Integration:

# -*- coding:utf-8 -*-
# @Time   : 2022-07-23
# @Author : carl_DJ
def online_open(url, mode):
    import requests
    r = (url)
    filename = ("/")[-1]
    with open(filename,'w') as f1:
        (("utf-8"))
    f2 = open(filename,'r')
    return f2

import fileinput
file_url = '/'
with (files=(file_url,), openhook=online_open) as file:
    for line in file:
        print(line, end="")

running result

summarize

That's all there is to say about fileinput.

fileinput itself is a re-wrapping of the open function, so the cc part of the read is much more specialized and elegant than open, which is limited to the read side.

In terms of writing, it's not so tough compared to OPEN.

When it comes down to it, fileinput is still a good method. It's worth having.

To this point, this article on the Python implementation of the method of reading files summarizes the article is introduced to this, more related to Python read file content, please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!