Whenever we need to check if file with specific extension exists in a directory or get list of files with specific extension from a directory, it can be done using python. Here is simple program for doing the same.
There are three ways to do this using python
- os.walk
- os.listdir
- glob.glob
All three methods are demonstrated in below program
Output of the program
*** Program Started *** List of Files using os.walk: ['file3.txt', 'list_to_file_in_directory.txt', 'test.txt'] List of Files using listdir: ['file3.txt', 'list_to_file_in_directory.txt', 'test.txt'] List of Files using glob : ['file3.txt', 'list_to_file_in_directory.txt', 'test.txt'] *** Program Completed ***