简体   繁体   中英

File exists but python says 'does not exist'

I'm running this code that reads 2 csv files (one of them is train.csv). The code gives an error saying 'file not does not exist'. However, the file does exists in the same location as the .py file. Can someone please help me on this. Thanks!

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

The variable is being referred to as ->

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)

You are opening a relative path, but your working directory is not what you think it is.

Use an absolute path instead:

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

Without an absolute path, Python uses the current working directory. What that directory is depends on how you ran your script, and is not something you should rely on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM