Python listing all files in directory
<p>Can anybody help me creating a function which will creating a list all files under certain directory by using pathlib library?</p>

<p>Here, I have a</p>

<p><a href="http://i.stack.imgur.com/jJMwB.png" rel="nofollow"><img src="http://i.stack.imgur.com/jJMwB.png" alt="enter image description here"></a></p>

<p>I have  </p>

<ul>
<li><p>c:\desktop\test\A\A.txt</p></li>
<li><p>c:\desktop\test\B\B_1\B.txt</p></li>
<li><p>c:\desktop\test\123.txt</p></li>
</ul>

<p>I expected to have a single list which would have path above, but my code return me nested list.</p>

<p>Here is my code:
from pathlib import Path</p>

<pre><code>def searching_all_files(directory: Path):   
    file_list = [] # A list for storing files existing in directories

    for x in directory.iterdir():
        if x.is_file():

           file_list.append(x)
        else:

           file_list.append(searching_all_files(directory/x))

    return file_list


p = Path('C:\\Users\\akrio\\Desktop\\Test')

print(searching_all_files(p))
</code></pre>

<p>Hope anybody could correct me. Thank you</p>

<pre><code>def searching_all_files(directory: Path):   
    file_list = [] # A list for storing files existing in directories

    for x in directory.iterdir():
        if x.is_file():
            file_list.append(x)#here should be appended
        else:
            file_list.extend(searching_all_files(directory/x))# need to be extended

    return file_list
</code></pre>

python pathlib