faithon faithon - 1 year ago 135
Python Question

Insert string to Dataframe in python 3.6.0

I want to find the folder path for all

.gz
files from network folder.
I have below
script
but its giving
error
:

TypeError: cannot concatenate a non-NDFrame object


Please help on the same

Script:

import os
import pandas as pd

adcPath = r'\\ADC\redshift-datasets\BLSCEWAG2016'

gzPath = pd.DataFrame(columns=['Path'], dtype=object)
for path, subdirs, files in os.walk(adcPath):
for name in files:
if name.endswith('.gz'):
gzPath = gzPath.append(path) # Want to insert to dataframe gzPath to export in csv

gzPath = gzPath['Path'].unique()
exportPath = r'D:\Sunil_Work\temp8' + '\\Path.csv'
gzPath.to_csv(exportPath)

Answer Source

You can use the glob function in the glob module to get all .gz files:

import glob
files = glob.glob(r'\\ADC\redshift-datasets\BLSCEWAG2016\**\*.gz', recursive=True)

Then, create the data frame and call df.unique:

gzPath = pd.DataFrame(files, columns=['Path'], dtype=object)['Path'].unique()

Save to csv:

exportPath = r'D:\Sunil_Work\temp8' + '\\Path.csv'
gzPath.to_csv(exportPath)    
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download