shantanuo
shantanuo

Reputation: 32326

Walking through directory path

I am trying to list all the directories (one level below /home/ dir) and it's contents. This code is working but when I convert the dictionary to pandas dataframe, the directory name does not match with file names.

mypath='/home/'
from os import walk

myd=dict()
for (dirpath, dirnames, filenames) in walk(mypath):
    for i in dirnames:
        for (dirpath1, dirnames1, filenames1) in walk(i):
            myd[i]=','.join(filenames1)


import pandas as pd
df=pd.DataFrame(myd , index=[0]).T
df.columns=['files']

pd.set_option('max_colwidth', 800)
df

Is there better way to build 2 column dataframe with directory and it's file contents?

Upvotes: 3

Views: 541

Answers (1)

hiro protagonist
hiro protagonist

Reputation: 46869

I'm not exactly sure what your end result should look like, but os.walk does the full recursion for you! There is no need to iterate over the dirnames in a second loop:

import os

mypath = '/home/'

myd = {}
for (here, dirs, files) in os.walk(mypath):
    for file in files:
        myd[here] = '.'.join(files)

print(myd)

This is python 3 code; it python 2 file is a keyword and should not be used as variable name...

UPDATE

if you only need one level below the input directory there is no need to walk:

myd = {}
for name in os.listdir(mypath):
    subdir = os.path.join(mypath, name)
    if not os.path.isdir(subdir):
        continue
    myd[name] = '.'.join(os.listdir(subdir))

print(myd)

Upvotes: 2

Related Questions