Python recursive directory file listing


















Now after combining our step function with the walk function, the script looks something like this:. The os. It takes 4 arguments, and only the first is mandatory. The arguments and their default values in order are:. The only one we are concerned with for now is the first. Aside from the arguments, perhaps the biggest difference in the two versions of the walk function is that the Python 2. This means that the Python 3. Instead of defining a separate function to call as with step we will write the os.

Like the Python 2. Other than that we won't change the extension suffix test at all, so the script ends up looking something like this:. This will work the same way for whatever the "topdir" and "exten" strings are set to; however, this script simply prints the filenames to the window in our examples the Python IDLE window , and if there are many files to print this leaves our interpreter or shell window many rows high—kind of a pain to scroll through.

If we know that this is the case, it would be much easier to write the results to a text file we can look at anytime. Scott Smith Scott Smith 10 10 silver badges 15 15 bronze badges.

Michael Silverstein Michael Silverstein 1, 14 14 silver badges 15 15 bronze badges. It looks like this code works for folders 2 levels or deeper only. Still it does get me closer. For each dir, starting from root it yields a 3-tuple dirpath, dirnames, filenames from os import walk from os. In Python 2. I tried your code and got a list with many repeats If you just remove lines under the comment " recursive calls on subfolders" - it works fine — borisbn.

I think the problem is that you're not processing the output of os. Dave Webb Dave Webb k 56 56 gold badges silver badges bronze badges. I have data in each sub directory, so I need to have a separate text file for the contents of each directory. Brock: the files part is the list of files in the current directory. So the indentation is indeed wrong.

Try this: import os import sys for root, subdirs, files in os. Diego Diego 10 10 bronze badges. Why would you do another listdir and then isdir when you already have the directory listing split into files and directories from walk?

Scott Scott 3, 3 3 gold badges 28 28 silver badges 48 48 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Helping communities build their own LTE networks.

Podcast Making Agile work for data science. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked No idea what made me write it like that. Thanks for mentioning it! I'll fix it. It will also give you a list of all nested subfolders at basically no cost.

You can search for several different extensions. You can also choose to return either full paths or just the names for the files by changing f. See below for a detailed speed anaylsis. None of the results use natural sorting.

This means results will be sorted like this: 1, 10, 2. Found files: Found subfolders: os. Found files: glob. Found files: pathlib. Found files: os. Found files: Tests were done with W7x64, Python 3. Great solution, but I had one issue with it that took me a bit to figure out.

I recommend adding a length check i. BrandonHunter, it does not return True. Keep in mind ext is a list and not a string. You can eliminate the recursive nature of this function by appending dir to subfolders at the beginning of the function and then adding an outer loop that iterates over subfolders. This should give a very small speed improvement, especially for very deep directory structures.

It also frees up the function's output in case you need to return something other than subfolders and files. Note that depending on the way you add and access elements of subfolders , the ordering of the output could be different. Looks like this is not true. On benchmarking your code snippet for larger dataset, it takes more time than that of the code that uses glob. However the code works as expected. Sudeepa Nadeeshan 2 2 silver badges 13 13 bronze badges.

Jefferson Lima Jefferson Lima 4, 2 2 gold badges 25 25 silver badges 25 25 bronze badges. This answer worked for me in Python 3.

We need this because we only have one instance of the current directory and a list containing the file names in the current directory - ultimately we want a list of full file paths not just the names of the files, since that wouldn't be of much use. Zip returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. Since the first list we provide is the current directory this will give us a list that looks something like this:.



0コメント

  • 1000 / 1000