简体   繁体   English

使用 glob 递归获取包含 CSV 的子目录和文件

[英]Using glob recursion to get sub directories and files containing CSVs

I am trying to concat multiple CSVs that live in subfolders of my parent directory.我正在尝试连接位于我父目录的子文件夹中的多个 CSV。

/ParentDirectory
│  
│
├───SubFolder 1
│       test1.csv
│
├───SubFolder 2
│       test2.csv
│
├───SubFolder 3
│       test3.csv
│       test4.csv
│
├───SubFolder 4
│       test5.csv

When I do当我做

import pandas as pd
import glob

files = glob.glob('/ParentDirectory/*.csv', recursive=True)
df = pd.concat([pd.read_csv(fp) for fp in files], ignore_index=True)

I get ValueError: No objects to concatenate .我得到ValueError: No objects to concatenate

But if I select a specific sub folder, it works:但是如果我 select 一个特定的子文件夹,它就可以工作:

files = glob.glob('/ParentDirectory/SubFolder 3/*.csv', recursive=True)

How come glob isn't able to go down a directory and get the CSVs within each folder of the parent directory?为什么glob无法通过 go 访问目录并在父目录的每个文件夹中获取 CSV?

Try:尝试:

files = glob.glob('/ParentDirectory/**/*.csv', recursive=True)
files = glob.glob('/ParentDirectory/*/*.csv')

It doesn't need to be recursive for that pattern, but does need a wildcard for the subdirectory.它不需要为该模式递归,但确实需要子目录的通配符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM