-1

我正在运行读取 2 个 csv 文件(其中一个是 train.csv)的代码。该代码给出了一个错误,提示“文件不存在”。但是,该文件确实与 .py 文件存在于同一位置。有人可以帮我解决这个问题。谢谢!

Reading dataset...
Traceback (most recent call last):
  File "c:\Project_1\regression_2.py", line 163, in <module>
    main(**args)
  File "c:\Project_1\regression_2.py", line 80, in main
    train_data = pd.read_csv(train)
  File "c:\Python27\lib\site-packages\pandas\io\parsers.py", line 401, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "c:\Python27\lib\site-packages\pandas\io\parsers.py", line 209, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "c:\Python27\lib\site-packages\pandas\io\parsers.py", line 509, in __init__
    self._make_engine(self.engine)
  File "c:\Python27\lib\site-packages\pandas\io\parsers.py", line 611, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "c:\Python27\lib\site-packages\pandas\io\parsers.py", line 893, in __init__
    self._reader = _parser.TextReader(src, **kwds)
  File "parser.pyx", line 312, in pandas._parser.TextReader.__cinit__
 (pandas\src\parser.c:2846)
  File "parser.pyx", line 512, in pandas._parser.TextReader._setup_parser_source
 (pandas\src\parser.c:4893)
IOError: File train.csv does not exist

该变量被称为 ->

def main(train='train.csv', test='test.csv', submit='logistic_pred.csv'):    
    print "Reading dataset..."
    train_data = pd.read_csv(train)
    test_data = pd.read_csv(test)
4

1 回答 1

8

您正在打开一个相对路径,但您的工作目录不是您认为的那样。

请改用绝对路径:

train = os.path.join('c:/Documents and Settings', train)

如果没有绝对路径,Python 将使用当前工作目录。该目录是什么取决于您运行脚本的方式,而不是您应该依赖的目录。

于 2013-07-01T10:57:56.470 回答