我想浏览同一目录中不同文件夹中包含的不同 csv 文件。我的文件夹位于我的工作目录中。我的文件夹命名为:
folder1, folder2,folder3
它们每个都有名称相同的 csv csv1.csv、csv2.csv
。
我尝试了这段代码:
import os
import re
import pandas as pd
from pandas.core.frame import DataFrame
rootDir = '.'
for dirName, subdirList, fileList in os.walk(rootDir, topdown=False):
print('Found directory: %s' % dirName)
for fname in fileList:
print('\t%s' % fname)
if "csv1.csv" == fname:
var= pd.read_csv(fname)
我可以打印该文件夹中 csv 文件的名称,但出现错误:
IOError:文件 csv1.csv 不存在
可能是什么问题?
最佳答案
正如您在注释中看到的,您必须加入 rootDir
、dirName
和 fname
。
import os
import re
import pandas as pd
from pandas.core.frame import DataFrame
rootDir = '.'
for dirName, subdirList, fileList in os.walk(rootDir, topdown=False):
print('Found directory: %s' % dirName)
for fname in fileList:
print('\t%s' % fname)
filepath = os.path.join(rootDir, dirName, fname)
if "csv1.csv" == fname:
var = pd.read_csv(filepath)
print var.head()
Join one or more path components intelligently. The return value is the concatenation of path and any members of
*paths
with exactly one directory separator (os.sep
) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.On Windows, the drive letter is not reset when an absolute path component (e.g.,
r'\foo'
) is encountered. If a component contains a drive letter, all previous components are thrown away and the drive letter is reset. Note that since there is a current directory for each drive,os.path.join("c:", "foo")
represents a path relative to the current directory on driveC:
(c:foo
), notc:\foo
.
关于python - 将多个文件中的多个CSV文件读取到pandas DataFrame中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32363154/