【问题标题】:`read_csv` doesn't see my file in Windows`read_csv` 在 Windows 中看不到我的文件
【发布时间】:2018-04-29 21:12:27
【问题描述】:

我是在 Windows 上使用 Pandas 的新手,我不确定我在这里做错了什么。

我的数据位于'C:\Users\me\data\lending_club\loan.csv'

path = 'C:\\Users\\me\\data\\lending_club\\loan.csv'
pd.read_csv(path)

我得到这个错误:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-107-b5792b17a3c3> in <module>()
      1 path = 'C:\\Users\\me\\data\\lending_club\\loan.csv'
----> 2 pd.read_csv(path)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision)
    707                     skip_blank_lines=skip_blank_lines)
    708 
--> 709         return _read(filepath_or_buffer, kwds)
    710 
    711     parser_f.__name__ = name

C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds)
    447 
    448     # Create the parser.
--> 449     parser = TextFileReader(filepath_or_buffer, **kwds)
    450 
    451     if chunksize or iterator:

C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, f, engine, **kwds)
    816             self.options['has_index_names'] = kwds['has_index_names']
    817 
--> 818         self._make_engine(self.engine)
    819 
    820     def close(self):

C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers.py in _make_engine(self, engine)
   1047     def _make_engine(self, engine='c'):
   1048         if engine == 'c':
-> 1049             self._engine = CParserWrapper(self.f, **self.options)
   1050         else:
   1051             if engine == 'python':

C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, src, **kwds)
   1693         kwds['allow_leading_cols'] = self.index_col is not False
   1694 
-> 1695         self._reader = parsers.TextReader(src, **kwds)
   1696 
   1697         # XXX

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source()

FileNotFoundError: File b'C:\\Users\\me\\data\\lending_club\\loan.csv' does not exist

编辑:

我重新安装了 Anaconda,错误消失了。不确定到底发生了什么,但可能与在我的第二次安装中初始安装是全局的还是特定于用户的有关。谢谢大家的帮助!

【问题讨论】:

  • 不确定这里出了什么问题。我只是使用相对路径。 - 这不可能吗?
  • 当您执行print(os.listdir(your_path)) 时会发生什么?

标签: windows pandas filepath


【解决方案1】:

Python 只能访问当前文件夹的文件。 如果你想从其他文件夹访问文件,试试这个:

import sys
sys.path.insert(0, 'C:/Users/myFolder')

这些行允许您的脚本访问其他文件夹。注意,你应该使用斜杠 /,而不是反斜杠 \

【讨论】:

    【解决方案2】:

    只需使用正斜杠('/') 而不是反斜杠('\')

    path = 'C:/Users/me/data/lending_club/loan.csv'
    

    【讨论】:

      猜你喜欢
      • 2017-06-01
      • 1970-01-01
      • 2012-12-22
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      相关资源
      最近更新 更多