Python uses glob to read multiple text files or excel files (read multiple files at once! Just specify the suffix)

Python uses glob to read multiple text files or excel files

I'm here to give you another module for Amway! It's called glob, but it doesn't need to be installed. It seems to come with os.
Really! Super easy to use!

To give you a simple example:
there are ten files in a folder, you may create a list, put these ten txt files in the list and then traverse to read
but!
glob can directly read files with a given suffix (such as txt) in the folder!
Attach the code (this code is very concise because I picked it out from a large section)

import os
import glob

# 确定打开的路径
input_path = "D:\python代码\gupiaoshuju\A+Hgu\lishihangqingshuju\\"

# 使用glob读取多个文本文件 这里选取的是excel的后缀
for i in glob.glob(os.path.join(input_path,"*.xlsx")):
    print(i)

This is the file contained in the folder
Insert picture description here

This is the output
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_50481455/article/details/109205690