pandas.read_csv在循环内给出FileNotFound错误

时间:2017-03-19 14:51:10

标签: pandas python-3.6

当用作单个语句时,

pandas.read_csv正常工作。但是当它在循环中使用时它正在给FileNotFoundError,即使该文件存在。

for filename in os.listdir("./Datasets/pollution"):
    print(filename) # To check which file is under processing
    df = pd.read_csv(filename, sep=",").head(1)

以上这些行给出了以下错误。

pollutionData184866.csv <----- The name of the file is printed properly.
Traceback (most recent call last):
  File "/home/parnab/PycharmProjects/FinalYearProject/locationExtractor.py", line 13, in <module>
    df = pd.read_csv(i, sep=",").head(1)
  File "/usr/lib/python3.6/site-packages/pandas/io/parsers.py", line 646, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/usr/lib/python3.6/site-packages/pandas/io/parsers.py", line 389, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "/usr/lib/python3.6/site-packages/pandas/io/parsers.py", line 730, in __init__
    self._make_engine(self.engine)
  File "/usr/lib/python3.6/site-packages/pandas/io/parsers.py", line 923, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "/usr/lib/python3.6/site-packages/pandas/io/parsers.py", line 1390, in __init__
    self._reader = _parser.TextReader(src, **kwds)
  File "pandas/parser.pyx", line 373, in pandas.parser.TextReader.__cinit__ (pandas/parser.c:4184)
  File "pandas/parser.pyx", line 667, in pandas.parser.TextReader._setup_parser_source (pandas/parser.c:8449)
FileNotFoundError: File b'pollutionData184866.csv' does not exist

但是当我在做的时候

filename = 'pollutionData184866.csv'
df = pd.read_csv(filename, sep=',')

工作正常。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

os.listdir("./Datasets/pollution")返回没有路径的文件列表,并根据路径"./Datasets/pollution"解析CSV文件,而不是当前目录".",因此将其更改为glob.glob('./Datasets/pollution/*.csv')应该工作,因为glob.glob()返回一个令人满意的文件/目录列表,包括给定的路径

演示:

In [19]: os.listdir('d:/temp/.data/629509')
Out[19]:
['AAON_data.csv',
 'AAON_data.png',
 'AAPL_data.csv',
 'AAPL_data.png',
 'AAP_data.csv',
 'AAP_data.png']

In [20]: glob.glob('d:/temp/.data/629509/*.csv')
Out[20]:
['d:/temp/.data/629509\\AAON_data.csv',
 'd:/temp/.data/629509\\AAPL_data.csv',
 'd:/temp/.data/629509\\AAP_data.csv']