简体   繁体   English

即使文件存在,文件 b'train.csv' 也不存在

[英]File b'train.csv' does not exist even though file exist

Code:代码:

import pandas as pd
train_df = pd.read_csv("train.csv")

Error:错误:

FileNotFoundError                   Traceback (most recent call last)
    <ipython-input-17-05c7c432b69f> in <module>()
      1 import pandas as pd
      2 
    ----> 3 train_df = pd.read_csv("../input/train.csv")

    /anaconda3/lib/python3.6/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

    /anaconda3/lib/python3.6/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:

    /anaconda3/lib/python3.6/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):

    /anaconda3/lib/python3.6/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':

    /anaconda3/lib/python3.6/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'../input/train.csv' does not exist

Please help!请帮忙! I tried using pd.read_csv("../input/train.csv") but there is still an error.我尝试使用pd.read_csv("../input/train.csv")但仍然有错误。 I am a Mac user using Jupyter notebook.我是使用 Jupyter 笔记本的 Mac 用户。

  1. Are you sure you have the correct path?你确定你有正确的路径吗?

train_df = pd.read_csv("./input/train.csv") (if the csv file is in the input folder which is in the same folder as your jupyter notebook) train_df = pd.read_csv("./input/train.csv") (如果 csv 文件位于输入文件夹中,该文件夹与您的 jupyter 笔记本位于同一文件夹中)

Easiest would be you have a folder which contains the juptyer notebook and the csv file.最简单的方法是您有一个包含 juptyer 笔记本和 csv 文件的文件夹。 Then you would just need to do:然后你只需要做:

train_df = pd.read_csv("./train.csv") or train_df = pd.read_csv("train.csv") train_df = pd.read_csv("./train.csv")train_df = pd.read_csv("train.csv")

  1. Try using train_df = pd.read_csv("train.csv",encoding='utf-8' )尝试使用train_df = pd.read_csv("train.csv",encoding='utf-8' )

to get rid of the 'b in front of b'../input/train.csv'摆脱 'b 前面的 b'../input/train.csv'

Try using an absolute path like this.尝试使用这样的绝对路径。 The r at the beginning of the line helps to read the whole string as a raw string as it is, so when r is used, you don't have to worry about escaping slashes该行开头的 r 有助于将整个字符串作为原始字符串读取,因此当使用 r 时,您不必担心转义斜杠

import pandas

myFile = pandas.read_csv(r"C:\Users\samarnat\Documents\Personal Docs\Projects\train.csv",encoding='utf-8')

Possible Reasons:可能的原因:

  1. Path entered is incorrect or multiple folders of same name.输入的路径不正确或有多个同名文件夹。
  2. The name of the file is incorrect.文件名不正确。
  3. The file extension is not csv , it maybe xlsx (Rare chance)文件扩展名不是csv ,它可能是xlsx (机会难得)

Hope this helps.希望这可以帮助。

尝试给出文件的绝对路径而不是相对路径。

I found, if you are using a new version of Excel extension, you will face the problem.我发现,如果您使用的是新版本的 Excel 扩展程序,您将面临这个问题。 First of all save the file eg.首先保存文件,例如。

  1. save as fileName = test另存为文件名 = 测试

  2. below name section select在名称部分选择下方

    • CSV UTF-8 (Comma Delimited )(*.csv) CSV UTF-8(逗号分隔)(*.csv)

now use Jupyter现在使用 Jupyter

import pandas as pd
test = pd.read_csv("test.csv")

检查文件字符串开头或结尾的 空格 字符。

I faced exact problem yesterday, following points helped me to resolve the problem我昨天遇到了确切的问题,以下几点帮助我解决了问题

  1. No combination filePath was working out for me.没有组合 filePath 适合我。
  2. I manually added full path to file and it worked for me.我手动添加了文件的完整路径,它对我有用。

So now I have added single line of code to my code and it works all the time regardless of folder structure.所以现在我在我的代码中添加了一行代码,无论文件夹结构如何,它都可以一直工作。 Providing absolute path of file is the key .提供文件的绝对路径是关键

csv_file_abs = os.path.abspath(csv_file)
print(f'ABS PATH: {csv_file_abs}')
df = pd.read_csv(csv_file_abs)

I hope this will help, please share your thoughts if this problem can be solved in other ways, this was quick-fix.我希望这会有所帮助,如果可以通过其他方式解决此问题,请分享您的想法,这是快速修复。

find out the directory you're working with using !pwd.使用 !pwd 找出您正在使用的目录。 Then upload the dataset in the same place.然后在同一个地方上传数据集。 Then copy and paste the path of the directory in pd.read_csv command然后将目录的路径复制粘贴到 pd.read_csv 命令中

    > df=pd.read_csv('/home/jovyan/demo/melb_data.csv')

what you're missing in the address is probably the jovyan term你在地址中缺少的可能是jovyan术语

if your text or csv file is in same folder where your jupyter notebook then instead of writing pd.read_csv('test.csv') write as pd.read_csv("test") bcz if your csv file explicitly shows .csv extension then only first way work or else second way.如果您的文本或 csv 文件位于您的 jupyter 笔记本所在的同一文件夹中,则不要编写 pd.read_csv('test.csv') 而是写为 pd.read_csv("test") bcz 如果您的 csv 文件明确显示 .csv 扩展名,则仅第一种方式工作,否则第二种方式。 for example if your file name looks like "test" then use pd.read_csv('test') or else filename is "test.csv" then use pd.read_csv('test.csv') It depend on saving name of file.例如,如果您的文件名看起来像“test”然后使用 pd.read_csv('test') 或者文件名是“test.csv”然后使用 pd.read_csv('test.csv') 这取决于保存文件的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM