Jupyter Noteb上带有Python和Windows的文件路径

2024-05-23 18:00:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法在Windows上的Jupyter笔记本上打开文件。

我在StackOverFlow的几个帖子中尝试了所有的建议(包括这个非常完整的帖子:Windows path in python

我的代码是这个:

today_date = date.today()
path2file = 'D:/Users/XXX123/My_folder/Project/Sub_folder/Relevés/audit/Forms_'+'{:%Y%m%d}'.format(today_date)+'.csv'
print(path2file)
pd.read_csv(os.path.normpath(path2file))

我收到一条错误信息:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-14-01089ab15706> in <module>()
      4 print(path2file)
      5 
----> 6 pd.read_csv(os.path.normpath(path2file))

~\AppData\Local\Continuum\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)
    653                     skip_blank_lines=skip_blank_lines)
    654 
--> 655         return _read(filepath_or_buffer, kwds)
    656 
    657     parser_f.__name__ = name

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds)
    403 
    404     # Create the parser.
--> 405     parser = TextFileReader(filepath_or_buffer, **kwds)
    406 
    407     if chunksize or iterator:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, f, engine, **kwds)
    762             self.options['has_index_names'] = kwds['has_index_names']
    763 
--> 764         self._make_engine(self.engine)
    765 
    766     def close(self):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\parsers.py in _make_engine(self, engine)
    983     def _make_engine(self, engine='c'):
    984         if engine == 'c':
--> 985             self._engine = CParserWrapper(self.f, **self.options)
    986         else:
    987             if engine == 'python':

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, src, **kwds)
   1603         kwds['allow_leading_cols'] = self.index_col is not False
   1604 
-> 1605         self._reader = parsers.TextReader(src, **kwds)
   1606 
   1607         # XXX

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__ (pandas\_libs\parsers.c:4209)()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source (pandas\_libs\parsers.c:8873)()

FileNotFoundError: File b'D:\\Users\\XXX123\\My_folder\\Project\\Sub_folder\\Relev\xc3\xa9s\\audit\\Forms20180123.csv' does not exist

我尝试了几种解决方案:斜杠、反斜杠、双反斜杠、三反斜杠、双引号或三引号、r'…',os.path.normpath('…'),。。。但我还是不能打开我的文件。

有人有主意吗?

注意:我的文件确实存在。。。

谢谢你的帮助。


Tags: inselfparserpandasdatelocalappdataengine
2条回答

尝试:

import datetime
import os
import pandas as pd
import sys

today_date = datetime.date.today()
path2file = r'D:/Users/XXX123/My_folder/Project/Sub_folder/Relevés/audit/Forms_'+'{:%Y%m%d}'.format(today_date)+'.csv'
# check that the file path exists
if not os.path.exists(path2file):
    print('{} not found'.format(path2file))
    sys.exit()
pd.read_csv(os.path.normpath(path2file))

exists检查您是否具有有效的文件路径。

将“r”符号放在文件路径之前可以确保解释器不会更改字符串,因为解释器认为其中一个反斜杠表示字符代码。

确保您的文件所在的文件夹可以在没有任何附加权限的情况下访问
如果您仍然获得了eror,只需将工作文件夹更改为desktop,即将项目文件夹移动到desktop并从那里加载它。这对我很有用,因为jupyter的默认启动位置也会给我同样的错误。

相关问题 更多 >