Python Forum

Full Version: Unable to create csv file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello

I am beginner. I am using python and tensorflow on IBM Data science experience.
I am facing a problem to create csv file (myFile.csv) in iris folder and get the error that file does not exist.

Path is correct. so Would you please guide me that why am I getting this error? so that I can solve it.

Thank you


==============
Code
==============

import os
import pandas as pd

path = "./data/samples/iris/"

filename_read = os.path.join(path,"myFile.csv")
print(filename_read)
os.path.exists("myFile.csv")
df = pd.read_csv(filename_read)
print(df)
=================
ERROR
==================
Error:
./data/samples/iris/myFile.csv --------------------------------------------------------------------------- OSError Traceback (most recent call last) <ipython-input-15-c68d1641f868> in <module>() 7 print(filename_read) 8 os.path.exists("myFile.csv") ----> 9 df = pd.read_csv(filename_read) 10 print(df) /usr/local/lib/python3.5/dist-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, skipfooter, 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, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision) 560 skip_blank_lines=skip_blank_lines) 561 --> 562 return _read(filepath_or_buffer, kwds) 563 564 parser_f.__name__ = name /usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds) 313 314 # Create the parser. --> 315 parser = TextFileReader(filepath_or_buffer, **kwds) 316 317 if (nrows is not None) and (chunksize is not None): /usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py in __init__(self, f, engine, **kwds) 643 self.options['has_index_names'] = kwds['has_index_names'] 644 --> 645 self._make_engine(self.engine) 646 647 def close(self): /usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py in _make_engine(self, engine) 797 def _make_engine(self, engine='c'): 798 if engine == 'c': --> 799 self._engine = CParserWrapper(self.f, **self.options) 800 else: 801 if engine == 'python': /usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py in __init__(self, src, **kwds) 1211 kwds['allow_leading_cols'] = self.index_col is not False 1212 -> 1213 self._reader = _parser.TextReader(src, **kwds) 1214 1215 # XXX pandas/parser.pyx in pandas.parser.TextReader.__cinit__ (pandas/parser.c:3427)() pandas/parser.pyx in pandas.parser.TextReader._setup_parser_source (pandas/parser.c:6861)() OSError: File b'./data/samples/iris/myFile.csv' does not exist
your code is trying to create a dataframe by read from a csv file (not creating one). the file ./data/samples/iris/myFile.csv does not exist (that is what error says)
also your line #8 os.path.exists("myFile.csv") has no visible effect - you don't print or use in any other way what os.path.exists will return.
Finally you 'check' if myFile.csv in current working directory, and this may or may not be the "./data/samples/iris/"
Thank You for help