0

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'],[....],[....]]
  • Does [this](https://stackoverflow.com/questions/9816816/get-absolute-paths-of-all-files-in-a-directory) answer your question? – tink Feb 14 '21 at 00:11

1 Answers1

1

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']]
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
  • Thanks for the answer. I was able to get that list after many iterations but i would like it to be like this, added an extra copy of img1 in sub1: [['/Users/name/Desktop/root/.DS_Store'], ['/Users/name/Desktop/root/sub1/img1.jpg', '/Users/name/Desktop/root/sub1/img1(copy).jpg'], ['/Users/name/Desktop/root/sub2/img2.jpg']] – Francisco Azocar Feb 14 '21 at 00:28
  • where every subfolder represent a list inside the main list – Francisco Azocar Feb 14 '21 at 00:33
  • Updated my code with 3 level nesting and multiple files in some folder – Akshay Sehgal Feb 14 '21 at 00:36
  • Let me know if that worked for you. Do mark the answer if it helped you and do upvote it if it’s well written to your liking. This encourages me to help solve your future questions :) – Akshay Sehgal Feb 14 '21 at 00:39
  • Thanks. If you have time just for better understanding. How would it look written using the old for x in y: format, i ask because the only way to get the output was setting the line in a variable? Thanks again. – Francisco Azocar Feb 14 '21 at 01:10