Printing a list of files from a directory
<p>I was wondering how to print a list of files from a directory. I know this should be very easy to do, but I'm blanking out on how to do it. My second method search characteristics(directory) is a method that should return the list of files found in that directory given a key press. The 3rd method take_action(directory1) should print the files returned by what you input under search_directory(directory) and then there should be more under that method later but for now let's focus on getting the list of files to print. </p>

<p>Here's what it should do.</p>

<p>The third line of the input specifies the action that should be taken on each of the interesting files found in the search. No matter what, you should always print the file's path, on its own line of output, to the console when you find an interesting one; the action chosen here specifies what else should be done with it.</p>

<p>Here's my code.</p>

<pre><code>import os
import os.path
import shutil
from pathlib import Path
import pathlib


def search_files():
    exist = Path(directory)
    if exist.exists():
        return directory
    else:
        print("Error")
        print("Try again: ")
        return search_files()





def search_characteristics(directory):

    interesting = input()
    interesting1=interesting.split(" ")
    if (interesting1[0] == 'N'):
        path1 = os.path.join(directory, interesting1[1])
        if(os.path.isfile(path1)):
            return path1
        else:
            return search_characteristics(directory)
        print(path1)
        return path1
    elif interesting1[0] == 'E':
        for file in os.listdir(directory):  
            if file.endswith(interesting1[1]): 
                return file
    elif interesting1[0] == 'S':
        for file in os.listdir(directory):
            try:
                if os.path.getsize(file) &gt; int(interesting1[1]):
                    return file
            except:
                print('Only Numbers after S please.')
                return search_characteristics(directory)

    else:
        print("Error")
        return search_characteristics(directory)

def take_action(directory1):
    action = input()
    action1=action.split(" ")
    if (action1[0] == 'P'):
        print(directory1)






if __name__ == '__main__':
    directory = input()
    search_files()
    directory1=search_characteristics(directory)
    take_action(directory1)
</code></pre>

<p>When I run it, it only seems to return the first file from the list of files that is supposed to be returned. I'm also not sure if I'm reading what it should do correctly. </p>

<p>Try <code>os.walk()</code></p>

<pre><code>list(os.walk("."))[0]
</code></pre>

<p>will give you all the sub directories in the current folder.</p>

<p>EDIT</p>

<p>Maybe this is more suited to your needs </p>

<pre><code>filter(lambda x : os.path.isdir(x) , os.listdir("."))
</code></pre>

python