In Python, how to find all files in a directory, including files in subdirectories?

Are there any built-in functions to search for all files under a specific directory, including files under subdirectories? I tried this code but didn't work ... maybe the logic itself is wrong ...

def fun(mydir): lis=glob.glob(mydir) length=len(lis) l,i=0,0 if len(lis): while(l+i<length): if os.path.isfile(lis[i]): final.append(lis[i]) lis.pop(i) l=l+1 i=i+1 else: i=i+1 print final fun(lis) else: print final 
+4
source share
3 answers

There is no built-in function, but using os.walk to create it is trivial:

 import os def recursive_file_gen(mydir): for root, dirs, files in os.walk(mydir): for file in files: yield os.path.join(root, file) 

ETA : the os.walk function resolves a directory tree recursively; the recursive_file_gen function is a generator (uses the yield keyword to create the next file). To get a list, follow these steps:

 list(recursive_file_gen(mydir)) 
+10
source

os.walk () is what you need.

But for extra performance, try the scandir package. It is also part of the standard library in Python 3.5 and is described in PEP 471.

+2
source

I highly recommend this path module, written by Jason Orendorf:

http://pypi.python.org/pypi/path.py/2.2

Unfortunately, his website is currently down, but you can download it from the above link (or via easy_install if you want).

Using this route module, you can perform various actions along the paths, including the required walk files. Here is an example:

 from path import path my_path = path('.') for file in my_path.walkfiles(): print file for file in my_path.walkfiles('*.pdf'): print file 

There are also convenient functions for many other things related to paths:

 In [1]: from path import path In [2]: my_dir = path('my_dir') In [3]: my_file = path('readme.txt') In [5]: print my_dir / my_file my_dir/readme.txt In [6]: joined_path = my_dir / my_file In [7]: print joined_path my_dir/readme.txt In [8]: print joined_path.parent my_dir In [9]: print joined_path.name readme.txt In [10]: print joined_path.namebase readme In [11]: print joined_path.ext .txt In [12]: joined_path.copy('some_output_path.txt') In [13]: print path('some_output_path.txt').isfile() True In [14]: print path('some_output_path.txt').isdir() False 

There are other operations that can be performed, but these are some of the ones that I use most often. Note that the path class inherits from string , so it can be used wherever string used. Also note that two or more path objects can be easily combined using the overridden / operator.

Hope this helps!

+2
source

Source: https://habr.com/ru/post/1310228/


All Articles