Reading all .csv files with different names

  • Context: MATLAB 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    files Reading
Click For Summary

Discussion Overview

The discussion revolves around the challenge of reading multiple .csv files with different names from a specified folder using programming techniques. The focus is on finding a method to automate the reading process without specifying each file name individually.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant describes an error encountered when attempting to read multiple .csv files using a wildcard in the file path.
  • Another participant suggests using the dir() function to list all .csv files in the directory, proposing a loop to load them one by one.
  • A third participant explains the underlying mechanics of the dir() function and its interaction with the Linux operating system, mentioning the importance of understanding UNIX or Linux for better control over file operations.
  • A later reply expresses gratitude for the suggestions and mentions efforts to improve Linux skills through simple shell scripts.

Areas of Agreement / Disagreement

Participants generally agree on the utility of using the dir() function to list files, but there is no consensus on the best approach to read multiple .csv files with different names, as the original error remains unresolved.

Contextual Notes

The discussion does not address specific error messages or the exact nature of the problem encountered with the initial attempt to read the files. There may be assumptions about the environment and file structure that are not explicitly stated.

Who May Find This Useful

This discussion may be useful for individuals working with data files in programming environments, particularly those interested in automating file reading processes in Python or similar languages, as well as those looking to improve their understanding of Linux commands.

member 428835
Hi PF!

I am trying to read all the .csv files in a folder. When I execute
Code:
OFdata  = readtable('/home/josh/Documents/NASA/PSI_DATA/Double_Drain/ICF1-9/OpenFOAM_DATA/*.csv');
I get a general error. However, replacing the asterisk with the file name, it load no issue.

I've seen online how to read several .csv files with only a numeric difference in name (for loops), but is there a way to read all .csv files with different names?
 
Last edited by a moderator:
Physics news on Phys.org
Code:
list  = dir('/home/josh/Documents/NASA/PSI_DATA/Double_Drain/ICF1-9/OpenFOAM_DATA/*.csv');
This should give you a list of all the csv files. You can then use a loop to load them one by one.
 
  • Like
Likes   Reactions: member 428835, jim mcnamara and jedishrfu
dir() calls the Linux OS - which globs the filenames it can find with the pattern: "*.csv". Then dir() executes stat() to get attributes -- on each file it finds. Like filetimes, file ownership, etc. - you can ask it do lots of tricks.

You can see the the one call python asks Linux to make for mulitiple objects. when you read the man page for glob. It works on filesystems and file, not what are categorized as python __dict__ objects.

From bash -- dir() is a window-ism for ls:
Code:
man glob
Code:
man ls

Most of the Linux OS is written in the C language, so part of the glob page shows how call it in your own C program. And explains lots details.

python acts as an intermediate for you.

But this is why I mentioned to you earlier about learning UNIX or Linux. Anytime an intermediate (interpreted usually) app does some OS good thing for you, you can control it better when you understand how something works underneath.

... followup from our earlier conversation.
 
  • Like
Likes   Reactions: member 428835
Thank you both! Very helpful, and Jim, I'm working on getting better with Linux. Starting with some simple shell scripts, baby steps.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 6 ·
Replies
6
Views
11K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K