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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With