List All the Files from All the Folder in a Single List

List all the files from all the folder in a single list

Try this:

 .....
List<File> files = getListFiles(new File("YOUR ROOT"));
....
private List<File> getListFiles(File parentDir) {
ArrayList<File> inFiles = new ArrayList<File>();
File[] files = parentDir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
inFiles.addAll(getListFiles(file));
} else {
if(file.getName().endsWith(".csv")){
inFiles.add(file);
}
}
}
return inFiles;
}

or variant without recursion:

private List<File> getListFiles2(File parentDir) {
List<File> inFiles = new ArrayList<>();
Queue<File> files = new LinkedList<>();
files.addAll(Arrays.asList(parentDir.listFiles()));
while (!files.isEmpty()) {
File file = files.remove();
if (file.isDirectory()) {
files.addAll(Arrays.asList(file.listFiles()));
} else if (file.getName().endsWith(".csv")) {
inFiles.add(file);
}
}
return inFiles;
}

Find all files in a directory with extension .txt in Python

You can use glob:

import glob, os
os.chdir("/mydir")
for file in glob.glob("*.txt"):
print(file)

or simply os.listdir:

import os
for file in os.listdir("/mydir"):
if file.endswith(".txt"):
print(os.path.join("/mydir", file))

or if you want to traverse directory, use os.walk:

import os
for root, dirs, files in os.walk("/mydir"):
for file in files:
if file.endswith(".txt"):
print(os.path.join(root, file))

Batch File; List files in directory, only filenames?

The full command is:

dir /b /a-d

Let me break it up;

Basically the /b is what you look for.

/a-d will exclude the directory names.


For more information see dir /? for other arguments that you can use with the dir command.

Python - How to find all same types of files in a folder and make a list out of it?

I prefer to use glob library :

One line can solve your problem :

import glob, os
glob.glob(os.path.join(outpath,"*.jpg"))

For your code , if you want to append element into a list, you must += a list.

jpg_file_list += [file] 

or use append

jpg_file_list.append(file)

How can I get a list of lists out a folder of files in python?

The problem in your code is just that you assign to alist:

alist = individualFiles.readlines()

instead of append to it:

alist.append(individualFiles.readlines())

And you will have to create a list before the loop: alist = list()

This your code, modified a little, to explain the logic:

from os import listdir

# Name of the folder containing the files
folder_path = "textfiles"

# Get a list of filenames
filenames = listdir(folder_path)

# List to store the content of the files
files_content = list()

# For each file
for filename in filenames:
# Create the filepath
file_path = f"{folder_path}/{filename}"

# Open the file (using "with" for file opening will autoclose the file at the end. It's a good practice)
with open(file_path, "r") as f:
# Get the file content
file_content = f.readlines()
# Append the conten to the list
files_content.append(file_content)

print(files_content)

List files ONLY in the current directory

Just use os.listdir and os.path.isfile instead of os.walk.

Example:

import os
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
# do something

But be careful while applying this to other directory, like

files = [f for f in os.listdir(somedir) if os.path.isfile(f)]

which would not work because f is not a full path but relative to the current directory.

Therefore, for filtering on another directory, do os.path.isfile(os.path.join(somedir, f))

(Thanks Causality for the hint)

How can I get the list of files in a directory using C or C++?

UPDATE 2017:

In C++17 there is now an official way to list files of your file system: std::filesystem. There is an excellent answer from Shreevardhan below with this source code:

#include <string>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;

int main()
{
std::string path = "/path/to/directory";
for (const auto & entry : fs::directory_iterator(path))
std::cout << entry.path() << std::endl;
}

Old Answer:

In small and simple tasks I do not use boost, I use dirent.h. It is available as a standard header in UNIX, and also available for Windows via a compatibility layer created by Toni Ronkko.

DIR *dir;
struct dirent *ent;
if ((dir = opendir ("c:\\src\\")) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
printf ("%s\n", ent->d_name);
}
closedir (dir);
} else {
/* could not open directory */
perror ("");
return EXIT_FAILURE;
}

It is just a small header file and does most of the simple stuff you need without using a big template-based approach like boost (no offence, I like boost!).



Related Topics



Leave a reply



Submit