从多个文件夹中读取* .csv作为列表 [英] Read *.csv as list from multiple folders

查看:90
本文介绍了从多个文件夹中读取* .csv作为列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多个文件夹中有数千个* csv文件 ../ t1 / *。csv ../ t2 / *。csv ../ t3 / *。csv ...等。



从以下多个文件夹中上传文件:

  filenames<  -  list.files(c(C:/ Example / t1 ,C:/ Example / t2),pattern =*。csv,full.names = TRUE)
list.df< - lapply(filenames,read.csv)

但是我必须输入所有目录 C:/ Example / t1 C:/ Example / t2 等如何读取所有数据(作为data.frames列表)与一个主目录有如: C :/ Example / *

解决方案

使用 list.files recursive = TRUE 将搜索第一个参数下的所有文件夹匹配文件:

 > list.files(./,recursive = TRUE)
[1]a / a1.csva / a2.csva / notme.txtb / b1.csv /e/e1.csv

这是我当前目录下的所有文件,如果我只想要CSV :

 > list.files(./,recursive = TRUE,pattern =*。csv)
[1]a / a1.csva / a2.csvb / b1.csv d / e / e1.csv

注意它在第二级的外观 d / e / 文件夹?



如果你只想去一个特定的深度,请尝试 Sys.glob - 这些模式匹配文件夹和文件,这些示例从当前目录工作:



只有第一级:

 > Sys.glob(* / *。csv)
[1]a / a1.csva / a2.csvb / b1.csv

只有第二级:

  Sys.glob(* / * / *。csv)
[1]d / e / e1.csv


I have thousands of *csv files in multiple folders ../t1/*.csv,../t2/*.csv,../t3/*.csv...etc.

I can upload the files from multiple folders as following:

filenames <- list.files(c("C:/Example/t1","C:/Example/t2"), pattern="*.csv", full.names=TRUE)
list.df <- lapply(filenames, read.csv)

However I have to type in all the directories C:/Example/t1, C:/Example/t2 etc. How to read all data (as list of data.frames) with one main directory somethign like: C:/Example/*?

解决方案

Using list.files with recursive=TRUE will search all folders under the first argument for matching files:

> list.files("./",recursive=TRUE)
[1] "a/a1.csv"    "a/a2.csv"    "a/notme.txt" "b/b1.csv"    "d/e/e1.csv" 

That's all the files under my current directory, if I only want CSVs:

> list.files("./",recursive=TRUE,pattern="*.csv")
[1] "a/a1.csv"   "a/a2.csv"   "b/b1.csv"   "d/e/e1.csv"

Notice how it looks in the second-level d/e/ folder?

If you only want to go to a single, specific depth, try Sys.glob - these patterns match folders and files and these examples work from the current directory:

Only first level:

> Sys.glob("*/*.csv")
[1] "a/a1.csv" "a/a2.csv" "b/b1.csv"

Only second level:

> Sys.glob("*/*/*.csv")
[1] "d/e/e1.csv"

这篇关于从多个文件夹中读取* .csv作为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆