This Python method of reading files is a ceiling level…

This Python method of reading files is a ceiling level…

Preamble
Hello brothers, today let’s learn about fileinput.

When it comes to fileinput, maybe 90% of the coders say they have never used it or even heard of it.

This is not surprising, because in the python world, since open can go all over the world, why do you need fileinput?

However, today I still want to introduce the fileinput method, because it is too nice.

More than just incense. It’s really fragrant! lcd display lcm

Next, just follow me, file input together, yes, this is the feel.

 

text
1. Method introduction
Basic usage

Let’s take a look at the basic functions of fileinput:

fileinput.filename(): Returns the filename currently being read.
—> Return None until the first line is read.

fileinput.fileno(): Returns the current file “file descriptor” as an integer.
—> When the file is not open (between the first line and the file), return -1.

fileinput.lineno(): Returns the cumulative line number that has been read.
—> Before the first line is read, return 0. After the last line of the last file has been read, returns the line number of that line.

fileinput.filelineno(): Returns the line number in the current file.
—> Before the first line is read, return 0.
–> After the last line of the last file has been read, returns the line number of that line in this file.

Advanced usage

fileinput.isfirstline(): Returns True if the line just read is the first line of the file it is in, otherwise returns False.

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

fileinput.nextfile(): 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 this file will not be counted toward the cumulative line count. The filename does not change 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 take effect.

fileinput.close(): Close the sequence.

2. Default read
code example

import fileinput

import fileinput

‘当 Python 脚本没有传入任何参数时,fileinput 默认会以 stdin 作为输入源’
for line in fileinput.input():
print(f'{line}’)

operation result

This Python method of reading files is a ceiling level...

What you enter, the program will read and output.

俗称:复读机
1
3. Process a file
code example

import fileinput

‘files 输入打开文件的名称即可’
with fileinput.input(files=(‘output.txt’,)) as file:
for line in file:
print(f'{fileinput.filename()} 第{fileinput.lineno()}行:{line}’,end=”)

import fileinput

‘files 输入打开文件的名称即可’
with fileinput.input(files=(‘output.txt’,)) as file:
for line in file:
print(f'{fileinput.filename()} 第{fileinput.lineno()}行:{line}’,end=”)

operation result

 

Parse:

fileinput has and only these two read modes: ‘r’, ‘rb’;
fileinput.input() uses mode=’r’ by default to read files. If your file is binary, you can use mode=’rb’.
4. Process batch files
Serial numbering of multiple files

call method

fileinput.lineno() method
code example

import fileinput

‘files 输入打开文件的名称即可’
with fileinput.input(files=(‘output.txt’,’input.txt’)) as file:
for line in file:
#fileinput.lineno() 把两个文件的整合陈一个文件对象file,需要排序输出
print(f'{fileinput.filename()} 第{fileinput.lineno()}行: {line}’, end=”)

# fileinput.filelineno()两个文件单独读取,需要单独排序
print(f'{fileinput.filename()} 第{fileinput.filelineno()}行: {line}’, end=”)

import fileinput

‘files 输入打开文件的名称即可’
with fileinput.input(files=(‘output.txt’,’input.txt’)) as file:
for line in file:
#fileinput.lineno() 把两个文件的整合陈一个文件对象file,需要排序输出
print(f'{fileinput.filename()} 第{fileinput.lineno()}行: {line}’, end=”)

# fileinput.filelineno()两个文件单独读取,需要单独排序
print(f'{fileinput.filename()} 第{fileinput.filelineno()}行: {line}’, end=”)

operation result

This Python method of reading files is a ceiling level

Multiple file serial numbers are sorted separately

call method

fileinput.filelineno() method
code example

import fileinput

‘files 输入打开文件的名称即可’
with fileinput.input(files=(‘test1.txt’,’test2.txt’)) as file:
for line in file:
# fileinput.filelineno()两个文件单独读取,需要单独排序
print(f'{fileinput.filename()} 第{fileinput.filelineno()}行: {line}’, end=”)

import fileinput

‘files 输入打开文件的名称即可’
with fileinput.input(files=(‘test1.txt’,’test2.txt’)) as file:
for line in file:
# fileinput.filelineno()两个文件单独读取,需要单独排序
print(f'{fileinput.filename()} 第{fileinput.filelineno()}行: {line}’, end=”)

operation result

This Python method of reading files is a ceiling level

Use with glob

In the era of beauty, the above output style can no longer meet our needs, so we thought of glob.

code example

import fileinput
import glob

#glob 匹配te开头的txt文件
for line in fileinput.input(glob.glob(“te*.txt”)):
if fileinput.isfirstline():
#输出读取文件
print(‘=’*10,f’读取文件{fileinput.filename()}’,’=’*10)
#fileinput.filelineno()方法读取
print(str(fileinput.filelineno())+ ‘:’+line.upper(),end=”)

import fileinput
import glob

#glob 匹配te开头的txt文件
for line in fileinput.input(glob.glob(“te*.txt”)):
if fileinput.isfirstline():
#输出读取文件
print(‘=’*10,f’读取文件{fileinput.filename()}’,’=’*10)
#fileinput.filelineno()方法读取
print(str(fileinput.filelineno())+ ‘:’+line.upper(),end=”)

operation result

With this beauty, which young lady can not like it.

5. Read and backup
call method

The backup parameter of fileinput.input can specify the suffix of the backup, such as .bak
code example

import fileinput

 

#触发backup的动作,源文件内容被修改,对源文件进行backup
with fileinput.input(files=(“test1.txt”,), backup=”.bak”,inplace=1) as file:
for line in file:
print(line.rstrip().replace(‘111111’, ‘222222’))
print(f'{fileinput.filename()} 第{fileinput.lineno()}行: {line}’, end=”)

operation result

This Python method of reading files is a ceiling level

6. Redirect replacement
Parse

In the above example, the inplace parameter is used to indicate whether to write the standard output result back to the file, which is not replaced by default.
Code example:

import fileinput

#触发backup的动作,源文件内容被修改,对源文件进行backup
with fileinput.input(files=(“test2.txt”,), inplace=True) as file:
print(“[INFO] task is started…”)
for line in file:
print(f'{fileinput.filename()} 第{fileinput.lineno()}行: {line}’, end=”)
print(“[INFO] task is closed…”)

import fileinput

#触发backup的动作,源文件内容被修改,对源文件进行backup
with fileinput.input(files=(“test2.txt”,), inplace=True) as file:
print(“[INFO] task is started…”)
for line in file:
print(f'{fileinput.filename()} 第{fileinput.lineno()}行: {line}’, end=”)
print(“[INFO] task is closed…”)

operation result

This Python method of reading files is a ceiling level

Note

By running the results, you can see:

The print content in the body of the for loop will be written back to the original file.
The print outside the for loop is unchanged.
7. Advanced
Analysis of the meaning of openhook

There is an openhook parameter in fileinput.input(), which supports the user to pass in a custom object reading method;
If no hook is passed in, fileinput uses the open function by default;
Method introduction

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 (identified by the extensions ‘.gz’ and ‘.bz2’);
If the file extension is not ‘.gz’ or ‘.bz2′, the file is opened in the normal way (ie with open() and without any decompression);
Example usage: fi = fileinput.FileInput(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.
Example usage: fi = fileinput.FileInput(openhook=fileinput.hook_encoded(“utf-8”, “surrogateescape”))
Example combat

If I want to use fileinput to read files on the network, the idea is:

First use requests to download the file to the local
Then use open to read it;
def online_open(url, mode):
import requests
r = requests.get(url)
filename = url.split(“/”)[-1]
with open(filename,’w’) as f1:
f1.write(r.content.decode(“utf-8”))
f2 = open(filename,’r’)
return f2
def online_open(url, mode):
import requests
r = requests.get(url)
filename = url.split(“/”)[-1]
with open(filename,’w’) as f1:
f1.write(r.content.decode(“utf-8″))
f2 = open(filename,’r’)
return f2

Just pass this function to openhook directly:

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

import fileinput
file_url = ‘https://www.csdn.net/robots.txt’
with fileinput.input(files=(file_url,), openhook=online_open) as file:
for line in file:
print(line, end=””)

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

import fileinput
file_url = ‘https://www.csdn.net/robots.txt’
with fileinput.input(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 = requests.get(url)
filename = url.split(“/”)[-1]
with open(filename,’w’) as f1:
f1.write(r.content.decode(“utf-8″))
f2 = open(filename,’r’)
return f2

import fileinput
file_url = ‘https://www.csdn.net/robots.txt’
with fileinput.input(files=(file_url,), openhook=online_open) as file:
for line in file:
print(line, end=””)

# -*- coding:utf-8 -*-
# @Time : 2022-07-23
# @Author : carl_DJ
def online_open(url, mode):
import requests
r = requests.get(url)
filename = url.split(“/”)[-1]
with open(filename,’w’) as f1:
f1.write(r.content.decode(“utf-8″))
f2 = open(filename,’r’)
return f2

import fileinput
file_url = ‘https://www.csdn.net/robots.txt’
with fileinput.input(files=(file_url,), openhook=online_open) as file:
for line in file:
print(line, end=””)

In the process of learning Python, my friends sometimes don’t know how to learn and where to start. After mastering some basic knowledge or doing some cases, I don’t know what to do next, and I don’t know how to learn more advanced knowledge.
So for these big brothers, I have prepared a lot of free video tutorials, PDF e-books, and source code!
Click the blue font to pick it up, I put it all here.
There will also be a big answer!

operation result

This Python method of reading files is a ceiling level

Summarize
The introduction to fileinput is also introduced here.

fileinput itself is a re-encapsulation of the open function, so in the cc part of reading, it is more professional and elegant than open, which is also limited to reading.
In terms of writing, compared to open, it is not so powerful.

At the end of the day, fileinput is still a good method. worthy of your possession.

Finally, I would like to recommend a set of Python crawler tutorials for you: Code always forgotten after learning? 100 crawler combat projects! Let you indulge in learning丨Apply what you learn丨The next Python god is you!