[英]FileNotFoundError on python3, even though the File does exist
[英]Trying to load file into Python but it states that it does not exist even though it does
我正在尝试将csv文件加载到Pandas中。 我收到一个从未出现过的奇怪错误,即使该文件不存在,该文件也不存在。 该错误还将消息“ ree.csv”中的文件命名为不同的名称
import pandas as pd
tree = pd.read_csv('C:\Users\Desktop\tree.csv')
IOError
Traceback (most recent call last)
<ipython-input-63-aed3b96442f2> in <module>()
----> 1 tree = pd.read_csv('C:\Users\Desktop\tree.csv')
C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\parsers.pyc in parser_f(filepath_or_buffer, sep, dialect, compression, doublequote, escapechar, quotechar, quoting, skipinitialspace, lineterminator, header, index_col, names, prefix, skiprows, skipfooter, skip_footer, na_values, na_fvalues, true_values, false_values, delimiter, converters, dtype, usecols, engine, delim_whitespace, as_recarray, na_filter, compact_ints, use_unsigned, low_memory, buffer_lines, warn_bad_lines, error_bad_lines, keep_default_na, thousands, comment, decimal, parse_dates, keep_date_col, dayfirst, date_parser, memory_map, nrows, iterator, chunksize, verbose, encoding, squeeze, mangle_dupe_cols, tupleize_cols)
398 )
399
--> 400 return _read(filepath_or_buffer, kwds)
401
402 parser_f.__name__ = name
C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\parsers.pyc in _read(filepath_or_buffer, kwds)
196
197 # Create the parser.
--> 198 parser = TextFileReader(filepath_or_buffer, **kwds)
199
200 if nrows is not None:
C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\parsers.pyc in __init__(self, f, engine, **kwds)
477 self.options['has_index_names'] = kwds['has_index_names']
478
--> 479 self._make_engine(self.engine)
480
481 def _get_options_with_defaults(self, engine):
C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\parsers.pyc in _make_engine(self, engine)
584 def _make_engine(self, engine='c'):
585 if engine == 'c':
--> 586 self._engine = CParserWrapper(self.f, **self.options)
587 else:
588 if engine == 'python':
C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\parsers.pyc in __init__(self, src, **kwds)
955 kwds['allow_leading_cols'] = self.index_col is not False
956
--> 957 self._reader = _parser.TextReader(src, **kwds)
958
959 # XXX
C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\parser.pyd in pandas.parser.TextReader.__cinit__ (pandas\parser.c:2987)()
C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\parser.pyd in pandas.parser.TextReader._setup_parser_source (pandas\parser.c:5345)()
IOError: File C:\Users\Desktop ree.csv does not exist
尝试这个:
'C:/Users/Desktop/tree.csv'
要么
'C:\\Users\\Desktop\\tree.csv'
问题是如何读取您的路径。
tree = pd.read_csv('C:\Users\Desktop\tree.csv')
由此, \\t
被视为tab
符 。
您有3种可用的解决方案:
选项1-使用原始字串:
tree = pd.read_csv(r'C:\Users\Desktop\tree.csv')
如果存在'r'或'R'前缀,则字符串中包含反斜杠后面的字符而不会更改,并且所有反斜杠都保留在字符串中。
选项2-使用双引号。 这样可以使\\
转义,以便您通过正确的路径。
tree = pd.read_csv('C:\\Users\\Desktop\\tree.csv')
选项3-将\\
更改为/
:
tree = pd.read_csv('C:/Users/Desktop/tree.csv')
所有这三个将提供可以利用的正确路径。
您没有为Python提供您认为的文件名,因为字符串中的反斜杠字符会被解释为特殊的转义字符。
使用不将反斜杠视为转义符的原始字符串:
r'C:\Users\Desktop\tree.csv'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.