Python Get Files In Directory Tutorial

Welcome to Python Get Files In Directory Tutorial. In this post, you will learn how to get files in directory using python. So let’s gets started this tutorial.

Python has various module such as os, os.path, shutil, pathlib etc by using which we can get files in directory. We will see how to work with these modules to get files.

Python Get Files In Directory – Getting Files With OS Module

In this section you will see how can you get files using OS module.

Directory Listing

OS module has two functions, by using which you can list your files.

  • os.listdir( )
  • os.scandir( )

os.listdir( )

  • os.listdir( ) is function of directory listing used in versions of Python prior to Python 3.
  • It returns a list containing the names of the entries in the directory given by path.

Let’s see an example of os.listdir( ) function. So write the following program.

Now let’s check the output, this will list all the files which are present in the specified directory.

Python Get Files In Directory
Python Get Files In Directory

You can see all the files which are in document folder has been listed.

os.scandir( )

  • It is a better and faster directory iterator.
  • scandir( ) calls the operating system’s directory iteration system calls to get the names of the files in the given path.
  • It returns a generator instead of a list, so that scandir acts as a true iterator instead of returning the full list immediately.
  • scandir( ) was introduced in python 3.5 .

Let’s see an example of os.scandir( ), so write the following code.

The output of the above code is following –

Python Get Files In Directory
Python Get Files In Directory

The ScandirIterator points to all the entries in the current directory.

If you want to print filenames then write the following code.

This will give you output as below.

Listing All Files In A Directory

Now you have to list all files in a directory that means printing names of files in a directory. So let’s write the following code.

  • When os.listdir( ) is called then it returns all the files and directory from the specified path.
  • os.path.isfile( ) is called to filter that list and it only prints files not directories.
  • You can see output below, here only files are printed.
Python Get Files In Directory
Python Get Files In Directory

Listing Sub-directories

Now to list sub-directories, you have to write following program.

  • os.path.isdir( ) to confirm that a given path points to a directory that means it returns true if path is an existing directory.

Let’s check the output –

Python Get Files In Directory
Python Get Files In Directory

Here you can see only sub-directories are listed.

Python Get Files In Directory – Getting Files With Pathlib Module

In this section, you will learn directory listing using pathlib module.

  • pathlib module offers classes representing filesystem paths with semantics appropriate for different operating systems.
  • Python 3.2 or later is recommended, but pathlib is also usable with Python 2.7 and 2.6.
  • So let’s see how can we do directory listing using pathlib module.

Directory Listing

Write the following code for directory listing using pathlib module.

  • First of all you have to import path class from pathlib module.
  • Then you have to create a path object that will return either PosixPath or WindowsPath objects depending on the operating system.
  • path.iterdir( ) return the path points to a directory, yield path objects of the directory contents. It is used to get a list of all files and directories of specified directory.
  • Then simply print the entry name.

Now check the output, let’s see what will it show.

Python Get Files In Directory
Python Get Files In Directory

Listing all Files In A Directory

Now let’s see how to list all files in a directory using pathlib module.

  • First of all call iterdir( ) method to get all the files and directories from the specified path.
  • Then start a loop and get all files using is_file( ) method. is_file( ) return True if the path points to a regular file, False if it points to another kind of file.
  • Then print all the files.

Now its time to check its output.

Python Get Files In Directory
Python Get Files In Directory

All files are listed successfully.

Listing Sub-directories

Write the following code to list subdirectories.

  • is_dir( ) is called to checks if an entry is a file or a directory, on each entry of the path iterator.
  • If it return True then the directory name is printed to the screen.
  • Now check the output.
Python Get Files In Directory
Python Get Files In Directory

Conclusion

In this tutorial, you have seen various ways of directory listing in python. OS and pathlib module is very useful in listing files. You have also seen many methods like listdir( ), scandir( ) and iterdir( ) that helps in getting files in directory.

So i am wrapping Python Get Files In Directory Tutorial here. I hope, you found very helpful informations about getting file in directory using python. But anyway, if you have any query then your queries are most welcome. I will try to solve out your issues. So just stay tuned with  Simplified Python and enhance your knowledge of python. Thanks everyone.

Leave a Comment