Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get all the csv file name under a directory?

Tags:

php

supposed there is a folder named example, and in it there are some csv file eg(a.csv, b.csv....).

the test.php directory is the same as example folder. now i want to pass all the csv file name to the following if condition. namely, replace test.csv with all the csv file name

if (($handle = fopen("test.csv", "r"))

how do i do?

i using the following code:

$files=  glob("./example/*.csv");
 if (($handle = fopen("$files", "r"))

but it doesn't work. thank you.

like image 997
down Avatar asked Aug 23 '12 09:08

down


People also ask

How do I list all CSV files in a directory in Python?

Method 1: Using Glob module glob(path). This returns all the CSV files' list located within the path. The regex used is equivalent to *. csv, which matches all files for an extension .

How do you list multiple CSV files in a folder?

In order to read multiple CSV files or all files from a folder in R, use data. table package. data. table is a third-party library hence, in order to use data.

How do you get a list of all files in a directory in Python?

To get a list of all the files and folders in a particular directory in the filesystem, use os. listdir() in legacy versions of Python or os. scandir() in Python 3.


1 Answers

$files is an array, you need to loop with it.

$files = glob("./example/*.csv");
foreach($files as $filepath) {
  if ($handle = fopen($filepath, "r")) {
     // ...
  }
}
like image 164
xdazz Avatar answered Oct 27 '22 02:10

xdazz