![](/img/trans.png)
[英]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.