- How to list files in a directory using the Windows API?
- 4 Answers 4
- Find all files in a folder
- 3 Answers 3
- Command to list all files in a folder as well as sub-folders in windows
- 6 Answers 6
- How to do a simple file search in cmd
- 5 Answers 5
- How do I list all files of a directory?
- 21 Answers 21
- Get a list of files with Python 2 and 3
- os.listdir() — list in the current directory
- Python 2
- Looking in a directory
- glob from glob
- glob im a list comprehension
- Getting the full path name with os.path.abspath
- Walk: going through sub directories
- os.listdir() : get files in the current directory (Python 2)
- To go up in the directory tree
- Get files: os.listdir() in a particular directory (Python 2 and 3)
- Get files of a particular subdirectory with os.listdir()
- os.walk(‘.’) — current directory
- next(os.walk(‘.’)) and os.path.join(‘dir’, ‘file’)
- next(os.walk(‘F:\’) — get the full path — list comprehension
- os.walk — get full path — all files in sub dirs**
- os.listdir() — get only txt files
- Using glob to get the full path of the files
- Using os.path.isfile to avoid directories in the list
- Using pathlib from Python 3.4
- Use glob method in pathlib.Path()
- Get all and only files with os.walk
- Get only files with next and walk in a directory
- Get only directories with next and walk in a directory
- Get all the subdir names with walk
- os.scandir() from Python 3.5 and greater
- Examples:
- Ex. 1: How many files are there in the subdirectories?
- Ex.2: How to copy all files from a directory to another?
- Ex. 3: How to get all the files in a txt file
- Example: txt with all the files of an hard drive
- All the file of C:\ in one text file
- How to write a file with all paths in a folder of a type
- (New) Find all files and open them with tkinter GUI
- Preliminary notes
- Solutions
- Programmatic approaches:
- Other approaches:
How to list files in a directory using the Windows API?
I have this code and it displays the folder with the directory itself and not its contents. I want to display its contents. I don’t want to use boost::filesystem.
How can I resolve this?
4 Answers 4
You got the directory because that’s what you asked for. If you want the files, ask for them:
(You can instead use *.* if you prefer, but apparently this only works because of a backwards compatibility hack so should probably be avoided. See comments and RbMm’s answer.)
Let me take some notes about «*.*» vs «*» . These filers are not equal.
2 different files can exist in our folder: somefile and somefile. .
If we used the low level api ZwQueryDirectoryFile with «*.*» as a search expression (this is the 10th parameter — FileName [in, optional] ) — we would get somefile. only. But if we used «*» we’d get both files — somefile and somefile.
If we try FindFirstFile(«C:\\semester2\\*.*», &data); we can note than both files somefile and somefile. are returned. So here «*.*» vs «*» have the same effect — no difference in usage.
Why does this happen? Because inside FindFirstFileEx in kernelbase ( kernel32 ) do special check for «*.*» mask and if it true — replace to «» (An empty name which have the same effect as «*» ).
I think this is done to fix a very common error when users pass «*.*» instead of the correct «*» and for backward compatability with legacy code.
. and .. aren’t actually part of the directory as it is stored on disk, but are added by the Win32 API.
This is not true.
- for FAT -style file system this is really stored on FAT directory as 2 first entry.
- in NTFS there are no such entries, but NTFS.sys artificially add this 2 entries if they in mask.
So this is done not at Win32 API level, but in kernel — on driver level.
In conclusion, «*.*» will work correct with Win32 API how minimum now — but the correct and clean way is to use «*» here.
«*.*» will be mistake with ZwQueryDirectoryFile api.
Find all files in a folder
I am looking to create a program that finds all files of a certain type on my desktop and places them into specific folders, for example, I would have all files with .txt into the Text folder.
Any ideas what the best way would be to accomplish this? Thanks.
I have tried this:
It was not successful in finding all of the files.
3 Answers 3
A lot of these answers won’t actually work, having tried them myself. Give this a go:
It will move all .txt files on the desktop to the folder TextFiles .
First off; best practice would be to get the users Desktop folder with
Then you can find all the files with something like
Note that with the above line you will find all files with a .txt extension in the Desktop folder of the logged in user AND all subfolders.
Then you could copy or move the files by enumerating the above collection like
Please note that you will have to include the filename in your Copy() (or Move() ) operation. So you would have to find a way to determine the filename of at least the extension you are dealing with and not name all the files the same like what would happen in the above example.
With that in mind you could also check out the DirectoryInfo and FileInfo classes. These work in similair ways, but you can get information about your path-/filenames, extensions, etc. more easily
Command to list all files in a folder as well as sub-folders in windows
I tried searching for a command that could list all the file in a directory as well as subfolders using a command prompt command. I have read the help for «dir» command but coudn’t find what I was looking for. Please help me what command could get this.
6 Answers 6
The below post gives the solution for your scenario.
/S Displays files in specified directory and all subdirectories.
/B Uses bare format (no heading information or summary).
/O List by files in sorted order.
If you want to list folders and files like graphical directory tree, you should use tree command.
There are various options for display format or ordering.
Check example output.
Answering late. Hope it help someone.
An addition to the answer: when you do not want to list the folders, only the files in the subfolders, use /A-D switch like this:
An alternative to the above commands that is a little more bulletproof.
It can list all files irrespective of permissions or path length.
I have a slight issue with the use of C:\NULL which I have written about in my blog
But nevertheless it’s the most robust command I know.
If you simply need to get the basic snapshot of the files + folders. Follow these baby steps:
- Press Windows + R
- Press Enter
- Type cmd
- Press Enter
- Type dir -s
- Press Enter
Following commands we can use for Linux or Mac. For Windows we can use below on git bash.
List all files, first level folders, and their contents
List all first-level subdirectories and files
How to do a simple file search in cmd
I want to quickly search for a file given its name or part of its name, from the windows command line (not power shell). This is similar to opening explorer and using the search box at the top.
Note: dir can search based on a string template but it will not search in the subdirectories.
Note2: findstr can be used to search for a token inside files and has a recursivity flag; it’s funny that a more complex find can be easily discovered .
5 Answers 5
dir /s *foo* searches in current folder and sub folders.
It finds directories as well as files.
/s Lists every occurrence of the specified file name within the specified directory and all subdirectories.
searches for all txt file in the directory tree. Before using it just change the directory to root using
you can also export the list to a text file using
and search within using
EDIT 1: Although this dir command works since the old dos days but Win7 added something new called Where
will search for exe & dll in the drive c:\Windows as suggested by @SPottuit you can also copy the output to the clipboard with
just wait for the prompt to return and don’t copy anything until then.
EDIT 2: If you are searching recursively and the output is big you can always use more to enable paging, it will show — More — at the bottom and will scroll to the next page once you press SPACE or moves line by line on pressing ENTER
How do I list all files of a directory?
How can I list all files of a directory in Python and add them to a list ?
21 Answers 21
os.listdir() will get you everything that’s in a directory — files and directories.
If you want just files, you could either filter this down using os.path :
or you could use os.walk() which will yield two lists for each directory it visits — splitting into files and dirs for you. If you only want the top directory you can break the first time it yields
I prefer using the glob module, as it does pattern matching and expansion.
It will return a list with the queried files:
Get a list of files with Python 2 and 3
os.listdir() — list in the current directory
With listdir in os module you get the files and the folders in the current dir
Python 2
Looking in a directory
glob from glob
with glob you can specify a type of file to list like this
glob im a list comprehension
Getting the full path name with os.path.abspath
You get the full path in return
Walk: going through sub directories
os.walk returns the root, the directories list and the files list, that is why I unpacked them in r, d, f in the for loop; it, then, looks for other files and directories in the subfolders of the root and so on until there are no subfolders.
os.listdir() : get files in the current directory (Python 2)
In Python 2, if you want the list of the files in the current directory, you have to give the argument as ‘.’ or os.getcwd() in the os.listdir method.
To go up in the directory tree
Get files: os.listdir() in a particular directory (Python 2 and 3)
Get files of a particular subdirectory with os.listdir()
os.walk(‘.’) — current directory
next(os.walk(‘.’)) and os.path.join(‘dir’, ‘file’)
next(os.walk(‘F:\\’) — get the full path — list comprehension
os.walk — get full path — all files in sub dirs**
os.listdir() — get only txt files
Using glob to get the full path of the files
If I should need the absolute path of the files:
Using os.path.isfile to avoid directories in the list
Using pathlib from Python 3.4
With list comprehension :
Alternatively, use pathlib.Path() instead of pathlib.Path(«.»)
Use glob method in pathlib.Path()
Get all and only files with os.walk
Get only files with next and walk in a directory
Get only directories with next and walk in a directory
Get all the subdir names with walk
os.scandir() from Python 3.5 and greater
Examples:
Ex. 1: How many files are there in the subdirectories?
In this example, we look for the number of files that are included in all the directory and its subdirectories.
Ex.2: How to copy all files from a directory to another?
A script to make order in your computer finding all files of a type (default: pptx) and copying them in a new folder.
Ex. 3: How to get all the files in a txt file
In case you want to create a txt file with all the file names:
Example: txt with all the files of an hard drive
All the file of C:\ in one text file
This is a shorter version of the previous code. Change the folder where to start finding the files if you need to start from another position. This code generate a 50 mb on text file on my computer with something less then 500.000 lines with files with the complete path.
How to write a file with all paths in a folder of a type
With this function you can create a txt file that will have the name of a type of file that you look for (ex. pngfile.txt) with all the full path of all the files of that type. It can be useful sometimes, I think.
(New) Find all files and open them with tkinter GUI
I just wanted to add in this 2019 a little app to search for all files in a dir and be able to open them by doubleclicking on the name of the file in the list.
will return a list of all files and directories in «somedirectory».
A one-line solution to get only list of files (no subdirectories):
or absolute pathnames:
Getting Full File Paths From a Directory and All Its Subdirectories
- The path I provided in the above function contained 3 files— two of them in the root directory, and another in a subfolder called «SUBFOLDER.» You can now do things like:
print full_file_paths which will print the list:
- [‘/Users/johnny/Desktop/TEST/file1.txt’, ‘/Users/johnny/Desktop/TEST/file2.txt’, ‘/Users/johnny/Desktop/TEST/SUBFOLDER/file3.dat’]
If you’d like, you can open and read the contents, or focus only on files with the extension «.dat» like in the code below:
Since version 3.4 there are builtin iterators for this which are a lot more efficient than os.listdir() :
According to PEP 428, the aim of the pathlib library is to provide a simple hierarchy of classes to handle filesystem paths and the common operations users do over them.
Note that os.walk() uses os.scandir() instead of os.listdir() from version 3.5, and its speed got increased by 2-20 times according to PEP 471.
Let me also recommend reading ShadowRanger’s comment below.
Preliminary notes
- Although there’s a clear differentiation between file and directory terms in the question text, some may argue that directories are actually special files
- The statement: «all files of a directory» can be interpreted in two ways:
- All direct (or level 1) descendants only
- All descendants in the whole directory tree (including the ones in sub-directories)
When the question was asked, I imagine that Python 2, was the LTS version, however the code samples will be run by Python 3(.5) (I’ll keep them as Python 2 compliant as possible; also, any code belonging to Python that I’m going to post, is from v3.5.4 — unless otherwise specified). That has consequences related to another keyword in the question: «add them into a list«:
- In pre Python 2.2 versions, sequences (iterables) were mostly represented by lists (tuples, sets, . )
- In Python 2.2, the concept of generator ([Python.Wiki]: Generators) — courtesy of [Python 3]: The yield statement) — was introduced. As time passed, generator counterparts started to appear for functions that returned/worked with lists
- In Python 3, generator is the default behavior
- Not sure if returning a list is still mandatory (or a generator would do as well), but passing a generator to the list constructor, will create a list out of it (and also consume it). The example below illustrates the differences on [Python 3]: map(function, iterable, . )
The examples will be based on a directory called root_dir with the following structure (this example is for Win, but I’m using the same tree on Lnx as well):
Solutions
Programmatic approaches:
Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order, and does not include the special entries ‘.’ and ‘..’ .
A more elaborate example (code_os_listdir.py):
Notes:
- There are two implementations:
- One that uses generators (of course here it seems useless, since I immediately convert the result to a list)
- The classic one (function names ending in _old)
- Recursion is used (to get into subdirectories)
- For each implementation there are two functions:
- One that starts with an underscore (_): «private» (should not be called directly) — that does all the work
- The public one (wrapper over previous): it just strips off the initial path (if required) from the returned entries. It’s an ugly implementation, but it’s the only idea that I could come with at this point
- In terms of performance, generators are generally a little bit faster (considering both creation and iteration times), but I didn’t test them in recursive functions, and also I am iterating inside the function over inner generators — don’t know how performance friendly is that
- Play with the arguments to get different results
Output:
Return an iterator of os.DirEntry objects corresponding to the entries in the directory given by path. The entries are yielded in arbitrary order, and the special entries ‘.’ and ‘..’ are not included.
Using scandir() instead of listdir() can significantly increase the performance of code that also needs file type or file attribute information, because os.DirEntry objects expose this information if the operating system provides it when scanning a directory. All os.DirEntry methods may perform a system call, but is_dir() and is_file() usually only require a system call for symbolic links; os.DirEntry.stat() always requires a system call on Unix but only requires one for symbolic links on Windows.
Notes:
- It’s similar to os.listdir
- But it’s also more flexible (and offers more functionality), more Pythonic (and in some cases, faster)
Generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple ( dirpath , dirnames , filenames ).
Notes:
- Under the scenes, it uses os.scandir ( os.listdir on older versions)
- It does the heavy lifting by recurring in subfolders
Return a possibly-empty list of path names that match pathname, which must be a string containing a path specification. pathname can be either absolute (like /usr/src/Python-1.5/Makefile ) or relative (like ../../Tools/*/*.gif ), and can contain shell-style wildcards. Broken symlinks are included in the results (as in the shell).
.
Changed in version 3.5: Support for recursive globs using “ ** ”.
Notes:
- Uses os.listdir
- For large trees (especially if recursive is on), iglob is preferred
- Allows advanced filtering based on name (due to the wildcard)
Notes:
- This is one way of achieving our goal
- It’s the OOP style of handling paths
- Offers lots of functionalities
ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.
Notes:
- It loads the three functions from libc (loaded in the current process) and calls them (for more details check [SO]: How do I check whether a file exists without exceptions? (@CristiFati’s answer) — last notes from item #4.). That would place this approach very close to the Python / C edge
- LinuxDirent64 is the ctypes representation of struct dirent64 from [man7]: dirent.h(0P) (so are the DT_ constants) from my machine: Ubtu 16 x64 (4.10.0-40-generic and libc6-dev:amd64). On other flavors/versions, the struct definition might differ, and if so, the ctypes alias should be updated, otherwise it will yield Undefined Behavior
- It returns data in the os.walk ‘s format. I didn’t bother to make it recursive, but starting from the existing code, that would be a fairly trivial task
- Everything is doable on Win as well, the data (libraries, functions, structs, constants, . ) differ
Output:
Retrieves a list of matching filenames, using the Windows Unicode API. An interface to the API FindFirstFileW/FindNextFileW/Find close functions.
Notes:
- win32file.FindFilesW is part of [GitHub]: mhammond/pywin32 — Python for Windows (pywin32) Extensions, which is a Python wrapper over WINAPIs
- The documentation link is from ActiveState, as I didn’t find any PyWin32 official documentation
- Install some (other) third-party package that does the trick
- Most likely, will rely on one (or more) of the above (maybe with slight customizations)
Notes:
Code is meant to be portable (except places that target a specific area — which are marked) or cross:
Multiple path styles (absolute, relatives) were used across the above variants, to illustrate the fact that the «tools» used are flexible in this direction
_get_dir_content (from point #1.) can be implemented using any of these approaches (some will require more work and some less)
- Some advanced filtering (instead of just file vs. dir) could be done: e.g. the include_folders argument could be replaced by another one (e.g. filter_func) which would be a function that takes a path as an argument: filter_func=lambda x: True (this doesn’t strip out anything) and inside _get_dir_content something like: if not filter_func(entry_with_path): continue (if the function fails for one entry, it will be skipped), but the more complex the code becomes, the longer it will take to execute
Nota bene! Since recursion is used, I must mention that I did some tests on my laptop (Win 10 x64), totally unrelated to this problem, and when the recursion level was reaching values somewhere in the (990 .. 1000) range (recursionlimit — 1000 (default)), I got StackOverflow :). If the directory tree exceeds that limit (I am not an FS expert, so I don’t know if that is even possible), that could be a problem.
I must also mention that I didn’t try to increase recursionlimit because I have no experience in the area (how much can I increase it before having to also increase the stack at OS level), but in theory there will always be the possibility for failure, if the dir depth is larger than the highest possible recursionlimit (on that machine)
The code samples are for demonstrative purposes only. That means that I didn’t take into account error handling (I don’t think there’s any try / except / else / finally block), so the code is not robust (the reason is: to keep it as simple and short as possible). For production, error handling should be added as well
Other approaches:
Use Python only as a wrapper
- Everything is done using another technology
- That technology is invoked from Python
The most famous flavor that I know is what I call the system administrator approach:
- Use Python (or any programming language for that matter) in order to execute shell commands (and parse their outputs)
- Some consider this a neat hack
- I consider it more like a lame workaround (gainarie), as the action per se is performed from shell (cmd in this case), and thus doesn’t have anything to do with Python.
- Filtering ( grep / findstr ) or output formatting could be done on both sides, but I’m not going to insist on it. Also, I deliberately used os.system instead of subprocess.Popen .
In general this approach is to be avoided, since if some command output format slightly differs between OS versions/flavors, the parsing code should be adapted as well; not to mention differences between locales).