Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get files in a directory, including all subdirectories

Tags:

python

People also ask

How do I get a list of files in a directory and subdirectories?

The dir command displays a list of files and subdirectories in a directory. With the /S option, it recurses subdirectories and lists their contents as well.

How do I list all subfolders in a directory?

Substitute dir /A:D. /B /S > FolderList. txt to produce a list of all folders and all subfolders of the directory. WARNING: This can take a while if you have a large directory.

How can I get a list of all the subfolders and files present in a directory using PHP?

PHP using scandir() to find folders in a directory The scandir function is an inbuilt function that returns an array of files and directories of a specific directory. It lists the files and directories present inside the path specified by the user.

How do I list all files in subdirectories?

By default, ls lists just one directory. If you name one or more directories on the command line, ls will list each one. The -R (uppercase R) option lists all subdirectories, recursively.


import os
import os.path

for dirpath, dirnames, filenames in os.walk("."):
    for filename in [f for f in filenames if f.endswith(".log")]:
        print os.path.join(dirpath, filename)

You can also use the glob module along with os.walk.

import os
from glob import glob

files = []
start_dir = os.getcwd()
pattern   = "*.log"

for dir,_,_ in os.walk(start_dir):
    files.extend(glob(os.path.join(dir,pattern))) 

Checkout Python Recursive Directory Walker. In short os.listdir() and os.walk() are your friends.


A single line solution using only (nested) list comprehension:

import os

path_list = [os.path.join(dirpath,filename) for dirpath, _, filenames in os.walk('.') for filename in filenames if filename.endswith('.log')]

I have a solution:

import os
for logfile in os.popen('find . -type f -name *.log').read().split('\n')[0:-1]:
      print logfile

or

import subprocess
(out, err) = subprocess.Popen(["find", ".", "-type", "f", "-name", "*.log"], stdout=subprocess.PIPE).communicate()
for logfile in out.split('\n')[0:-1]:
  print logfile

These two take the advantage of find . -type f -name *.log.

The first one is simpler but not guaranteed for white-space when add -name *.log, but worked fine for simply find ../testdata -type f (in my OS X environment).

The second one using subprocess seems more complicated, but this is the white-space safe one (again, in my OS X environment).

This is inspired by Chris Bunch, in the answer https://stackoverflow.com/a/3503909/2834102


If You want to list in current directory, You can use something like:

import os

for e in os.walk(os.getcwd()):
    print e

Just change the

os.getcwd()

to other path to get results there.