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>First, let me presume to offer a bit of very general advice. Get yourself some software that enables you to try out small snippets of code, if you don't have that already. On Windows the best I know of is PythonWin; you're looking for a REPL program. I mention this because I notice you were very close to an answer with the mention of Path in your code. If you had experimented a little with that you would have been on top of the answer to you principal question.</p>

<p>Getting a list of files in a directory:</p>

<pre><code>import os
from pathlib import Path
path=Path('C:\\Python34')
for fileName in path.iterdir():
    fileName.name
</code></pre>

<p>In response to one or two of the comments: Rather than depending on globals and on user input the usual practice is to fall back on a default. In this case that would be the current directory. To do this, make None the default directory and check whether it has been declared in functions; use current working directory otherwise. </p>

<pre><code>def search_files(directory=None):
if not directory:
    directory=os.getcwd
</code></pre>

python