Recursively list all files in directory (Unix)

Sairah George

I am trying to list all files a directory recursively using python. I saw many solutions using os.walk. But I don't want to use os.walk. Instead I want to implement recursion myself.

import os
fi = []
def files(a):
        f =  [i for i in os.listdir(a) if os.path.isfile(i)]
        if len(os.listdir(a)) == 0:
                return
        if len(f) > 0:
                fi.extend(f)
        for j in [i for i in os.listdir(a) if os.path.isdir(i)]:
                files(j)


files('.')
print fi

I am trying to learn recursion. I saw following Q?A, but I am not able to implement correctly it in my code.

Elisha

os.listdir return only the filename (without the full path) so I think calling files(j) will not work correctly.
try using files(os.path.join(dirName,j))

or something like this:

def files(a):
    entries =  [os.path.join(a,i) for i in os.listdir(a)]

    f =  [i for i in entries if os.path.isfile(i)]
    if len(os.listdir(a)) == 0:
            return
    if len(f) > 0:
            fi.extend(f)
    for j in [i for i in entries if os.path.isdir(i)]:
            files(j)

I tried to stay close to your structure. However, I would write it with only one loop over the entries, something like that:

def files(a):
    entries =  [os.path.join(a,i) for i in os.listdir(a)]
    if len(entries) == 0:
            return

    for e in entries:
        if os.path.isfile(e):
            fi.append(e)

        elif os.path.isdir(e):
            files(e)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Recursively list all files in directory (Unix)

From Dev

Recursively process files in a directory in unix

From Dev

How to recursively list all files of desired file type in a specified directory?

From Dev

Recursively apply a command to modify all files in a directory

From Dev

Remove all but N files from directory recursively

From Dev

Recursively list files from a given directory in Bash

From Dev

List files in directory recursively and what they contain

From Dev

How can I recursively list Md5sum of all the files in a directory and its subdirectories?

From Dev

Command line to search all files in all text files in directory recursively

From Dev

UNIX shell scripting: how to recursively move files up one directory?

From Dev

List all files with dates recursively with PHP

From Dev

Recursively list all files in eclipse workspace programmatically

From Dev

Recursively list all files in eclipse workspace programmatically

From Dev

How to recursively remove all but list of files?

From Dev

How to recursively list all hidden files and directories?

From Dev

Recursively list all files and subdirectories with modified timestamp

From Dev

List all files with dates recursively with PHP

From Dev

Recursively do something with all files in a given directory including subdirectory files

From Dev

How to copy all files in directory recursively and unzip compressed files on fly

From Dev

converting all files in a directory in unix to another format

From Dev

how to search for a line match from all files recursively found in a directory

From Dev

Recursively apply the msgfmt command to all .po files in directory with find -exec

From Dev

How to search and remove ^M in all files in a directory recursively?

From Dev

PHP: rename all files to lower case in a directory recursively

From Dev

How do I copy all files recursively to a flat directory in Ruby?

From Dev

Recursively generate script tags for all JavaScript files within a directory with PHP

From Dev

How to use grep on all files non-recursively in a directory?

From Dev

How can I move all files in subdirectories recursively to a single directory?

From Dev

How to use grep on all files non-recursively in a directory?

Related Related

  1. 1

    Recursively list all files in directory (Unix)

  2. 2

    Recursively process files in a directory in unix

  3. 3

    How to recursively list all files of desired file type in a specified directory?

  4. 4

    Recursively apply a command to modify all files in a directory

  5. 5

    Remove all but N files from directory recursively

  6. 6

    Recursively list files from a given directory in Bash

  7. 7

    List files in directory recursively and what they contain

  8. 8

    How can I recursively list Md5sum of all the files in a directory and its subdirectories?

  9. 9

    Command line to search all files in all text files in directory recursively

  10. 10

    UNIX shell scripting: how to recursively move files up one directory?

  11. 11

    List all files with dates recursively with PHP

  12. 12

    Recursively list all files in eclipse workspace programmatically

  13. 13

    Recursively list all files in eclipse workspace programmatically

  14. 14

    How to recursively remove all but list of files?

  15. 15

    How to recursively list all hidden files and directories?

  16. 16

    Recursively list all files and subdirectories with modified timestamp

  17. 17

    List all files with dates recursively with PHP

  18. 18

    Recursively do something with all files in a given directory including subdirectory files

  19. 19

    How to copy all files in directory recursively and unzip compressed files on fly

  20. 20

    converting all files in a directory in unix to another format

  21. 21

    how to search for a line match from all files recursively found in a directory

  22. 22

    Recursively apply the msgfmt command to all .po files in directory with find -exec

  23. 23

    How to search and remove ^M in all files in a directory recursively?

  24. 24

    PHP: rename all files to lower case in a directory recursively

  25. 25

    How do I copy all files recursively to a flat directory in Ruby?

  26. 26

    Recursively generate script tags for all JavaScript files within a directory with PHP

  27. 27

    How to use grep on all files non-recursively in a directory?

  28. 28

    How can I move all files in subdirectories recursively to a single directory?

  29. 29

    How to use grep on all files non-recursively in a directory?

HotTag

Archive