File exists but Python says 'not found'.

Discussion in 'Python' started by prerit86@gmail.com, Jul 1, 2013.

  1. Guest

    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!

    Code Output-->

    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\sr
    c\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
    , Jul 1, 2013
    #1
    1. Advertising

  2. Robert Kern Guest

    On 2013-07-01 11:47, wrote:
    > 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!


    How are you running the code? If you are doing pandas.read_csv('train.csv'), the
    file must be in the current working directory of the running process. The
    location of the .py file contain the code is not relevant. For example, if you
    are using an IDE to run the script, you may need to configure how it runs the
    script to pick a particular directory for it to run in.

    > Code Output-->
    >
    > 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)


    Since the filename of regression_2.py in the traceback is fully-qualified, I
    expect that you are running the program from something other than the
    c:\Project_1\ directory.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
    Robert Kern, Jul 1, 2013
    #2
    1. Advertising

  3. Guest

    The variable 'train' is being called like this ->

    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)

    Let me know if I need to post the full code.
    , Jul 1, 2013
    #3
  4. Op 01-07-13 12:47, schreef:
    > 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!
    >
    > Code Output-->
    >
    > 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\sr
    > c\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


    My guess is that train.csv is a symbolic link that points to a file
    that doesn't exist. So looking at your directory you can see an entry
    but actualy trying to open it will fail.

    --
    Antoon Pardon
    Antoon Pardon, Jul 1, 2013
    #4
  5. Guest

    I got it. The working directory was different. Sorry, I'm new and didn't the working directory has to be the location of the data. I thought the location of .py file and data file should be same. Thanks! Es. Robert Kern.
    , Jul 1, 2013
    #5
  6. Op 01-07-13 12:57, schreef:
    > The variable 'train' is being called like this ->
    >
    > 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)
    >
    > Let me know if I need to post the full code.


    I think Robert wanted to know how you started the program.
    What instruction do you use to launch?
    In what directory are you launching your program?
    Is that the same directory where train.csv is present?
    Antoon Pardon, Jul 1, 2013
    #6
  7. Guest

    My answers

    I think Robert wanted to know how you started the program.
    What instruction do you use to launch?
    - used command c:\python27\python.exe c:\project_1\code.py

    In what directory are you launching your program?
    - working directory was c:
    - python is in c:\python27
    - code was in c:\project_1
    - changed the working directory to c:\project_1 now and it worked

    Is that the same directory where train.csv is present?
    - train.csv is present in c:\project_1
    , Jul 1, 2013
    #7
  8. Dave Angel Guest

    On 07/01/2013 07:00 AM, wrote:
    > I got it. The working directory was different. Sorry, I'm new and didn't the working directory has to be the location of the data. I thought the location of .py file and data file should be same. Thanks! Es. Robert Kern.
    >


    Python didn't make that assumption, the author of the script did. By
    using a relative name like train.csv, he is implicitly forcing Python to
    use the current working directory.

    If he had wanted the file to be found in the same directory as one of
    the source files, he could have built an absolute path by using the
    __file__ attribute of the module.

    However, for non-const files, the source directory is generally a bad
    place to put them.

    Confounding things is the strange habit that Windows has of deciding
    that the script location *should* be your current directory. I believe
    Explorer does that if you right-click select a script.


    --
    DaveA
    Dave Angel, Jul 1, 2013
    #8
  9. Guest

    I know. Had I written the code, I would have not done this. I just wanted to get some initial results by leveraging this code. I would now build on this to improve my work's accuracy.

    Thanks for the inputs!
    , Jul 1, 2013
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Totan
    Replies:
    0
    Views:
    899
    Totan
    Apr 17, 2006
  2. keithb
    Replies:
    2
    Views:
    7,982
    keithb
    Jun 7, 2006
  3. Help Needed
    Replies:
    1
    Views:
    258
    Jason M. Murray
    Sep 19, 2003
  4. Divya
    Replies:
    2
    Views:
    177
    Bullschmidt
    May 11, 2004
  5. Replies:
    8
    Views:
    435
Loading...

Share This Page