How to get all the file in a directory

Find all the files in a directory

module os
There are a lot of way to do it, let’s start with the first.

Using os.listdir()

You just have to import the os module and you can use the os.listdir method (showing it on the screen with print, for example).

 

 

Get a list with the files (lista con tutti i file)

I have made also a short video here: Video (un video su questo argomento)

os.listdir(): get files in current dir (Python 3)

The simplest way to have the file in the current dir in Python 3 is this. It’s really simple, use the os module and the listdir() function and you’ll have the file in that dir (and eventual folders that are in the dir, but you will not have the file in the subdirectory, for that you can use walk – I will talk about it later).

Getting the full path name

As you noticed, you don’t have the full path of the file in the code above. If you need to have the absolute path, you can use another function of the os.path module called _getfullpathname, putting the file that you get from os.listdir() as an argument. There are other ways to have the full path, as we will check later (I replaced, as suggested by mexmex, _getfullpathname with abspath).

Get the full path name of a type of file into all subdirectories with walk

I find this very useful to find stuff in many directories and it helped me finding a file about which I didn’t remember the name:

os.listdir(): get files in current dir (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

glob module – all files

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

glob – get only txt files

Using glob to get the full path of the files

If I should need the absolute path of the files:

Other use of glob

If I want all the files in the directory:

Using os.path.isfile to avoid directories in the list*

Using pathlib from (Python 3.4)

If you want to use list comprehension

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 on


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 dir 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: create a txt with all the files names in your 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.

Looking for a type of file in all Hard Drive with os.walk