Can anybody help me create a function which will create a list of all files under a certain directory by using pathlib
library?
Here, I have a:
I have
c:\desktop\test\A\A.txt
c:\desktop\test\B\B_1\B.txt
c:\desktop\test\123.txt
I expected to have a single list which would have the paths above, but my code returns a nested list.
Here is my 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))
Hope anybody could correct me.