I want to get a list of all files in a target directory including subdirectories, with the path starting at the target directory.
edit1: I want to insert it at the correct position alphabetically.
$ tree test
test
├── file
└── sub
├── fileinsub
├── otherfileinsub
└── sub2
└── fileinsub2
>>> print(os.listdir("test"))
['sub', 'file']
>>>print(recusivelistdir("test"))
['sub', 'sub/fileinsub', 'sub/otherfileinsub', 'sub/sub2', 'sub/sub2/fileinsub2' 'file']
edit7: My working solution!!
def recursivelistdir(target):
target_files = []
for dir, subdir, files in os.walk(target):
for i in range(len(files)):
target_files.append((os.path.join(dir, files[i])).removeprefix(f"{target}/"))
target_files.append((dir).removeprefix(f"{target}/"))
# Remove the duplicate entry
target_files = list(dict.fromkeys(target_files))
if target_files.__contains__(target):
target_files.remove(target)
return target_files
print(recursivelistdir("test")) # ['file', 'sub/fileinsub', 'sub', 'sub/otherfileinsub', 'sub/sub2/fileinsub2', 'sub/sub2']
CodePudding user response:
import os
def recusivelistdir(path):
result = []
for root,dirs,files in os.walk(path):
result.extend([root os.path.sep _dir for _dir in dirs])
result.extend([root os.path.sep file for file in files])
print(result)
recusivelistdir('path folder')
CodePudding user response:
You can always made a recursive solution, but in this case the os
module have a good method, os.walk(path)
, where path
is the root directory
li=[]
for d,ds,fs in os.walk(path):
li =[f"{d}/{f}" for f in fs]
return li
Inside the for, d
will be the current directory, ds
a list of all subdirectories inside d
and fs
the list of all files inside d
, in every iteration of the for, the method "walks" inside the subdirectories inside the current directory.
CodePudding user response:
Use glob you will love it!
from glob import glob
target = "test" <REG_EX>
target_files = glob(target, recursive=True)
Replace REG_EX with whatever the sub_dir string you need.
To elaborate more on the solution use sorted
on glob output to sort the directories any type necessary
sorted(glob.glob('./test/**/*',recursive=True)) #Sorts alphabetically.
If you need more sorting;
sorted(glob.glob(('./test/**/*',recursive=True),key=os.path.getmtime) # Sorts with mt time
sorted(glob.glob(('./test/**/*',recursive=True),key=os.path.getsize) # Sorts with size