Francisco Azocar
Francisco Azocar

Reputation: 47

How to get a list of list of subfolder files with full path?

I would like to get the same list structure that i am getting in this approach but i get a full list instead which i would have to break down manually and it kills the "automate the task".

For example, I have a folder called test with 4 subfolders called A,B,C,D and inside each folder we can find files file1, file2, file3.

import os
import openpyxl
#Create a regex that matches files with the american date format
path = r'C:\Users\Dell_G7_15\Desktop\test'
pathWalk = os.walk(path)
fileIndex = os.listdir(path)
wb = openpyxl.Workbook()
i=0
filenames = []
filesPathLink=[]
for foldernames in pathWalk:
    filenames.append(foldernames[-1])   #creating list of filenames
    i= i+1
filenames.pop(0) #delete first value of the list that causes error 
print(filenames)

When i print filenames i get:

[['file1', 'file2', 'file3'],['file1', 'file2', 'file3'],['file1', 'file2', 'file3']]

I am looking for the same list structure but to get the full path of each one and it would look like this:

[['../A/file1', '../A/file2', '../A/file3'],[....],[....]]

Upvotes: 0

Views: 89

Answers (1)

Akshay Sehgal
Akshay Sehgal

Reputation: 19322

Is this what you are looking for?

For the following folder and sub folders -

# root/
#    -img0.jpg
#    sub1/
#       -img1.jpg
#       -img1 copy.jpg
#    sub2/
#       -img2.jpg
#       subsub1/
#           -img3.jpg

path = '/Users/name/Desktop/root'

[[r+'/'+fname for fname in f] for r,d,f in os.walk(path)]
[['/Users/name/Desktop/root/img0.jpg'],
 ['/Users/name/Desktop/root/sub1/img1 copy.jpg',
  '/Users/name/Desktop/root/sub1/img1.jpg'],
 ['/Users/name/Desktop/root/sub2/img2.jpg'],
 ['/Users/name/Desktop/root/sub2/subsub1/img3.jpg']]

For completion sake, if anyone is looking for a flat list of all files with paths inside a multi-level folder structure then try this -

[r+'/'+fname for r,d,f in os.walk(path) for fname in f]
['/Users/name/Desktop/root/img0.jpg',
 '/Users/name/Desktop/root/sub1/img1 copy.jpg',
 '/Users/name/Desktop/root/sub1/img1.jpg',
 '/Users/name/Desktop/root/sub2/img2.jpg',
 '/Users/name/Desktop/root/sub2/subsub1/img3.jpg']

EDIT: Simple loop without a list comprehension

filepaths = []
for r,d,f in os.walk(path):
    l = []
    for fname in f:
        l.append(r+'/'+fname)
    filepaths.append(l)

print(filepaths)
[['/Users/name/Desktop/root/img0.jpg'],
 ['/Users/name/Desktop/root/sub1/img1 copy.jpg',
  '/Users/name/Desktop/root/sub1/img1.jpg'],
 ['/Users/name/Desktop/root/sub2/img2.jpg'],
 ['/Users/name/Desktop/root/sub2/subsub1/img3.jpg']]

Upvotes: 1

Related Questions