Read *.csv as list from multiple folders

Maximilian

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/*?

Spacedman

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"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Azure Data Factory Read Csv Files from multiple portioned folders

Delete Folders from CSV List

read multiple text files from multiple folders

R: read a csv file from multiple folders and write a xslx file keeping the sheet names

Read images from two different folders and list the image directories with name in a csv file

How to read Images from multiple folders?

Read from CSV into a list with DictReader

How to read multiple images from multiple folders in python

Open/read multiple .tif images from multiple folders on different levels

Read and extract multiple PDF's from multiple folders using python

how to combine multiple CSV files from multiple folders in Python?

Reading multiple CSV files from multiple folders in Python?

How to read a specific paragraph from from multiple folders and files

Read from an Excel spreadsheet, and rename multiple folders with data from XLSX

Unix copy multiple folders based on a specified CSV list using rsync

Get data from CSV to delete users folders from multiple servers

Move a List of Folders from csv File without losing the permissions

How to read multiple records from a CSV file?

List of Timestamps read as string from CSV File

Python: Read list of dataframe from csv file

Read from CSV file as dictionary and append it into a list

QT Multiple item selection from a list of files and folders

Python - Create multiple folders from an excel column list

Loop over multiple folders from list with glob.glob

Read multiple folders files java

Recursively read files from sub-folders into a list and merge each sub-folder's files into one csv per sub-folder

Read matrices from multiple .csv files and print matrices in .csv files

Read and Append List From Multiple Files

Read dataframe from list in multiple RData files