Listing of all files in directory?
<p>Can anybody help me create a function which will create a list of all files under a certain directory by using <code>pathlib</code> library?</p>

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

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

<p>I have  </p>

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

<p>I expected to have a single list which would have the paths above, but my code returns a nested list.</p>

<p>Here is my code:</p>

<pre><code>from pathlib import Path

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.</p>