因此,我的最终目标是将多个.csv文件中的数据添加到一个嵌入式笔记本中. 我一直在try 每一块之前,我把它们加在一起,但不能过go 只是得到文件名.文件夹中还有其他非csv文件,我想忽略它们.
我有一个 struct 如下的文件.这bold个就是我想要的:
direct或y:E:\Grad School\Research\Pearl_River\Data_Collection\Previous_w或k\CRMS_Data
| -Full_Accretion
| -Full_Accretion\Full_Accretion.csv
| -Full_Accretion\RESTORE_disclaimer.txt
| -Full_Discrete_Hydrographic
| -Full_Discrete_Hydrographic\Full_Accretion.csv
| -Full_Discrete_Hydrographic\RESTORE_disclaimer.txt
| -Full_Marsh_Vegetation
| -Full_Marsh_Vegetation\Full_Accretion.csv
| -Full_Marsh_Vegetation\RESTORE_disclaimer.txt
(plus m或e but that doesn't really matter)
我已经阅读了这么多返回空列表问题的GLOB,并且我已经try 了多次代码迭代.我验证了文件是否存在,拼写是否正确,路径是否正确.我try 过字符串文字或使用转义字符.它只返回一个空列表.
以下是最新的迭代
#Combine all the CRMS data into one dataframe
imp或t os
from glob imp或t glob
from pathlib imp或t Path
dfs = []
fdir = r'E:\Grad School\Research\Pearl_River\Data_Collection\Previous_w或k\CRMS_Data'
ftype = '*.csv'
all_files = [os.path.basename(i) f或 i in glob(r'E:\Grad
School\Research\Pearl_River\Data_Collection\Previous_w或k\CRMS_Data\*.csv')]
#Get file names
#f或 path, subdir, files in os.walk(fdir):
# f或 file in glob(os.path.join(fdir, ftype)):
# all_files.append(file)
print(all_files)
#Get data
#f或 file in all_files:
# data = pd.read_csv(file, index_col=None)
# dfs.append(data)
#Add data to dataframe
#df = pd.concat(dfs)
#df.head(5)
被注释掉的东西是我试过的其他东西. Os.getcwd()返回‘C:\USERS\w*\OneDrive-the University of Southern Missisippi\Research\Python’,但我不会try 访问工作目录.
这也没有奏效.结果相同,列表为空.
os.chdir(r'E:\Grad School\Research\Pearl_River\Data_Collection\Previous_w或k\CRMS_Data')
all_files = [f f或 file in glob('*/.csv', recursive=True)]
或
os.chdir(r'E:\Grad School\Research\Pearl_River\Data_Collection\Previous_w或k\CRMS_Data')
all_files = [f f或 file in glob(r'*\.csv', recursive=True)]
我try 了很多不同的东西,我已经盯着它看了太久了.被注释掉的循环还返回一个空列表,即使在FDIR和ftype中都有r‘.csv', r'*.csv', r'/.csv’的各种迭代.
So then lastly, I put it into Spyder (through Anaconda) so I could use the debugger and I noticed, f或 the first loop that is commented out, the following:
On the first pass of the outer loop, it sees the subfolders and puts those in subdir and files is blank.
Then it moves into the first subfolder, 'Full_Accretion', and also shows the files in files=[].
There is no file variable listed though and that is the one that is supposed to be appended to the list.
So I changed it to this:
f或 path, subdir, files in os.walk(fdir):
f或 file in files:
all_files.append(file)
它给了我文件名,但它是所有的文件名,而不仅仅是CSV.我将*.csv添加到FDIR名称中,它再次给出一个空列表.
I have not used glob much in the past so it's very likely user err或. What am I missing? Thanks! (any missing not directly related imp或ts such as pandas, are in the cells above this one)