2017-07-27 45 views
1

我想從網絡文件夾中找到所有.gz文件的文件夾路徑。 我有以下script但它給人error將字符串插入到python 3.6.0中的Dataframe中

TypeError: cannot concatenate a non-NDFrame object 

在同一

腳本請幫幫忙:

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) 

回答

1

您可以使用glob功能glob模塊中獲取所有.gz文件:

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

然後,創建數據幀,並調用df.unique

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

保存到CSV:

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

@coldspeed,感謝響應。但我可以給路徑:adcPath = r'\\ ADC \ redshift-datasets \ BLSCEWAG2016',我想要從提到的路徑中獲取並非全部 –

+0

但是,如果子文件夾存在,它會給出空白,它只有在我給直接文件夾鏈接包含.gz文件 –

+0

@faithon好吧...編輯我的答案。用'glob'使用'遞歸'標誌。 –