我有一个包含大量文件夹的目录,我想分别从每个文件夹中的所有文件创建一个笛卡尔列表.所以每个文件夹都会有自己的笛卡尔列表.
import pandas as pd
import os, glob, itertools
path =(r'C:\pathway')
allfiles = glob.glob(path + "/*.csv")
result = list(itertools.product(allfiles,allfiles))
path =(r'C:\pathway')
for subdir, dirs, files in os.walk(path):
for file in files:
df=pd.read_csv(os.path.join(subdir,file))
解决方法:
glob支持多个通配符,因此您可以通过执行以下操作来完成笛卡尔积:
from glob import glob
from os.path import join
from itertools import product
BASE_PATH = 'C:\pathway'
all_files = glob(join(BASE_PATH, '*', '*.csv')) # C:\pathway\*\*.csv
result = list(product(all_files, all_files))
从docs(强调我的):
pathname can be either absolute (like
/usr/src/Python-1.5/Makefile
) or relative (like../../Tools/*/*.gif
), and can contain shell-style wildcards
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。