- How to get list of all files in directory and its sub-directories?
- Python – Get the list of all files in a directory and its sub-directories recursively
- Examples
- 1. Get the list of all files in given directory recursively
- 2. Get the list of all files with a specific extension in given directory
- Summary
- Listing out directories and files in Python
- Python Directory Listing
- 1. Python Directory Listing Using os.listdir()
- 2. Use os.path.join() with os.listdir()
- 3.Python Directory Listing Using os.walk()
- Conclusion
- References
How to get list of all files in directory and its sub-directories?
Python – Get the list of all files in a directory and its sub-directories recursively
To get the list of all files in a folder/directory and its sub-folders/sub-directories, we will use os.walk() function. The os.walk() function yields an iterator over the current directory, its sub-folders, and files.
In this tutorial, we shall go through some of the examples, that demonstrate how to get the list of all files in a directory and its sub-directories.
Examples
1. Get the list of all files in given directory recursively
In this example, we will take a path of a directory and try to list all the files in the directory and its sub-directories recursively.
Python Program
import os path ="C:/workspace/python" #we shall store all the file names in this list filelist = [] for root, dirs, files in os.walk(path): for file in files: #append the file name to the list filelist.append(os.path.join(root,file)) #print all the file names for name in filelist: print(name)
We have used nested Python For Loop in the above program.
C:\pythonexamples\python-create-directory.png C:\pythonexamples\python-remove-file.png C:\pythonexamples\scatter-plot-example.py C:\pythonexamples\tkinter-example.py C:\pythonexamples\sample\example.py C:\pythonexamples\sample\example1.py
2. Get the list of all files with a specific extension in given directory
In this example, we will take a path of a directory and try to list all the files, with a specific extension .py here, in the directory and its sub-directories recursively.
Python Program
import os path ="C:\workspace\python" for root, dirs, files in os.walk(path): for file in files: if(file.endswith(".py")): print(os.path.join(root,file))
C:\pythonexamples\scatter-plot-example.py C:\pythonexamples\tkinter-example.py C:\pythonexamples\sample\example.py C:\pythonexamples\sample\example1.py
Summary
In this tutorial of Python Examples, we learned how to get the list of all files in a directory and its sub-directories.
Listing out directories and files in Python
The following is a list of some of the important methods/functions in Python with descriptions that you should know to understand this article.
- len() – It is used to count number of elements(items/characters) of iterables like list, tuple, string, dictionary etc.
- str() – It is used to transform data value(integers, floats, list) into string.
- abspath() – It returns the absolute path of the file/directory name passed as an argument.
- enumerate() – Returns an enumerate object for the passed iterable that can be used to iterate over the items of iterable with an access to their indexes.
- list() – It is used to create a list by using an existing iterable(list, tuple, dictionary, set).
- listdir() – It is used to list the directory contents. The path of directory is passed as an argument.
- isfile() – It checks whether the passed parameter denotes the path to a file. If yes then returns True otherwise False
- isdir() – It checks whether the passed parameter denotes the path to a directory. If yes then returns True otherwise False
- append() – It is used to append items on list.
Please see the below code executed on interactive Python terminal to have a quick walk through on the usages of above functions/methods.
Path structure on different OS
Windows uses \ (back slash) as a path separator, eg. C:\Users\Desktop\
Linux based system like MAC OS X, Linux uses / (forward slash), eg. /Users/Desktop/
Let’s have a quick overview the working of above methods and functions as we are using this in our final program.
5 9 [1, 3, 67, 45, 83, 59] [1, 3, 67, 45, 83, 59] A list. 2017 2017 A year. 0 1 1 3 2 67 3 45 4 83 5 59 /Users/admin/projects/Python/PythonFiles True True ['city', 'age', 'name'] [2, 34, 6, 8, 10] [2, 34, 6, 8, 10, 98, 64] PythonPythonPython ####################
There are number of Python files & directories inside /Users/admin/projects/Python/PythonFiles, we will list out all these.
Suppose current working directory is /Users/admin/projects/Python/Django/E-Commerce-projects/ecommerce-2/src, which has some files and folders inside it.
In your case, it will be different. You will only need to pass the exact path of the directory that you want to list out.
The following is the python code to display all the files and directories based on the passed absolute or relative path.
If path is not specified in the calling statement then the contents of current working directory will be displayed.
5 files of /Users/admin/projects/Python/Django/E-Commerce-projects/ecommerce-2/src =================================================================================== 1) .gitignore 2) db.sqlite3 3) manage.py 4) requirements.txt 5) todo.txt 5 directories of /Users/admin/projects/Python/Django/E-Commerce-projects/ecommerce-2/src ========================================================================================= 1) ecommerce2 2) newsletter 3) products 4) static_in_pro 5) templates 70 files of /Users/admin/projects/Python/PythonFiles ===================================================== 1) 2_list_iterators.py 2) app.py 3) class_script_exec.py 4) class_variables.py 5) date_and_time.py 6) datetime.txt 7) dict.py 8) dictionary.py 9) django_home.html 10) error_handling.py 11) error_handling_output.py 12) error_handling_output.txt 13) execution_pickle.py 14) fb_task.py 15) for.py 16) gfg_sum_of_primes_in_numbers.py 17) hackerrank_numbers.py 18) hck_addition_aint_simple.py 19) hck_biased_chandan.py 20) hck_c_counts.py 21) hck_c_counts2.py 22) hck_c_counts3.py 23) hck_cool_numbers.py 24) hck_earth_fans.py 25) hck_earth_fans_2.py 26) hck_earth_fans_3.py 27) hck_earth_fans_final_on_28_dec_2016.py 28) hck_Little_Jhool_and_psychic_powers.py 29) hck_lonely_monk.py 30) hck_lonely_monk_orig.py 31) hck_maximum_AND.py 32) hck_min_max_problem.py 33) hck_monk_and_power_of_time.py 34) hck_numbers_rotation.py 35) hck_palindomic_numbers.py 36) hck_print_hackerearth.py 37) hck_print_hackerearth_2_way.py 38) hck_range_query.py 39) hck_recursive_functions.py 40) hck_recursive_sums.py 41) hck_strange_addition.py 42) hck_sum_of_numbers.py 43) interactive_img_resolutions.txt 44) json.py 45) json.pyc 46) katyperry.py 47) lambda_expression.py 48) linked_list_delete_nodes_at_front.py 49) linked_list_delete_nodes_at_front_output.txt 50) linked_list_is_palindrome_gfg.py 51) linked_list_is_palindrome_gfg_output.text 52) linked_list_is_palindrome_gfg_testing.py 53) linked_list_node_deletion_from_any_position.txt 54) linked_list_node_deletion_from_end.py 55) linked_list_node_deletion_from_end_output.txt 56) linked_list_node_deletion_from_middle.py 57) linked_list_node_insertion_at_beginning.py 58) linked_list_node_insertion_at_end.py 59) linked_list_node_insertion_at_middel_output.txt 60) linked_list_node_insertion_at_middle.py 61) map.py 62) merge_lists.py 63) mufeez_android_interview.py 64) python_for_loops.py 65) python_for_loops2.py 66) remove_dupliates.py 67) show_dir_and_files.py 68) show_dir_and_files_test.py 69) smarika_urllib_python2.7.10.py 70) while.py 2 directories of /Users/admin/projects/Python/PythonFiles ========================================================== 1) socket_programming 2) wx
This article is contributed by Rishikesh Agrawani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Python Directory Listing
In this article, we’ll look at how we can perform Python directory listing. This will allow us to list all the files and directories in the current working location.
Often. we may want to quickly look at the filenames and get information using Python.
Let’s look at how we can do it quickly and easily!
1. Python Directory Listing Using os.listdir()
This is a short and sweet method to perform Python directory listing, from your current directory!
It’s really just one line. Don’t believe me? Here’s an example. This applies to any operating system, whether it be Windows / Linux / MacOS.
Example Output
>>> import os >>> os.listdir() ['.bashrc', '.git', '.nvimrc', '.vimrc', '.xinitrc', '.zshrc', 'Autumn.jpg', 'README.md', 'config']
This will return a list of all files and nested folders, from your current directory.
If you want to specify an exact path, you can simply pass it as an argument to os.listdir(path) !
>>> os.listdir(r'/home/vijay/manjaro-dotfiles') ['.bashrc', '.git', '.nvimrc', '.vimrc', '.xinitrc', '.zshrc', 'Autumn.jpg', 'README.md', 'config']
Use raw strings (strings prefixed with r ) when you’re dealing with paths, since you won’t need to escape any backslashes (for Windows paths).
2. Use os.path.join() with os.listdir()
If you want to print the absolute path of all the files from your current directory, simply add an os.path.join() to the os.listdir() function!
We’ll make a function for this, which simply gets the full path, and returns a list of all such names.
import os def list_full_paths(directory): return [os.path.join(directory, file) for file in os.listdir(directory)] print(list_full_paths(r'/home/accornition/manjaro-dotfiles'))
['/home/vijay/manjaro-dotfiles/.bashrc', '/home/vijay/manjaro-dotfiles/.git', '/home/vijay/manjaro-dotfiles/.nvimrc' , '/home/vijay/manjaro-dotfiles/.vimrc', '/home/vijay/manjaro-dotfiles/.xinitrc', '/home/vijay/manjaro-dotfiles/.zsh rc', '/home/vijay/manjaro-dotfiles/Autumn.jpg', '/home/vijay/manjaro-dotfiles/README.md', '/home/vijay/manjaro-dotfiles/config']
Indeed, this gives us the absolute path, from the root directory!
3.Python Directory Listing Using os.walk()
We can also use the os.walk() function to walk through the directory tree.
We can then print the directories and files individually.
for top, dirs, files in os.walk(os.getcwd()): print("Printing directories. ") for dir in dirs: print(os.path.join(top, dir)) print("Printing files. ") for file in files: print(os.path.join(top, file))
Printing directories. /home/vijay/manjaro-dotfiles/config/cmus /home/vijay/manjaro-dotfiles/config/compton /home/vijay/manjaro-dotfiles/config/termite Printing files. Printing directories. Printing files. /home/vijay/manjaro-dotfiles/config/cmus/my.theme Printing directories. Printing files. /home/vijay/manjaro-dotfiles/config/compton/compton.conf Printing directories. Printing files. /home/vijay/manjaro-dotfiles/config/termite/config
You can use any of the above three methods, depending on your use-case scenario.
The first method is the easiest and the recommended one, but if you want the full path, and want to travel recursively, use os.walk() .
Conclusion
In this article, we learned how we could list files and directories in Python, using different methods.