OSError: Initializing from file failed on csv in Pandas

Question:

So far pandas read through all my CSV files without any problem, however now there seems to be a problem..

When doing:

df = pd.read_csv(r'path to file', sep=';')

I get:

OSError Traceback (most recent call
last) in ()
—-> 1 df = pd.read_csv(r’path
ÜbersichtInputtesttest.csv’, sep=’;’)

c:program filespython36libsite-packagespandasioparsers.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)
703 skip_blank_lines=skip_blank_lines)
704
–> 705 return _read(filepath_or_buffer, kwds)
706
707 parser_f.name = name

c:program filespython36libsite-packagespandasioparsers.py in
_read(filepath_or_buffer, kwds)
443
444 # Create the parser.
–> 445 parser = TextFileReader(filepath_or_buffer, **kwds)
446
447 if chunksize or iterator:

c:program filespython36libsite-packagespandasioparsers.py in
init(self, f, engine, **kwds)
812 self.options[‘has_index_names’] = kwds[‘has_index_names’]
813
–> 814 self._make_engine(self.engine)
815
816 def close(self):

c:program filespython36libsite-packagespandasioparsers.py in
_make_engine(self, engine) 1043 def _make_engine(self, engine=’c’): 1044 if engine == ‘c’:
-> 1045 self._engine = CParserWrapper(self.f, **self.options) 1046 else: 1047 if engine == ‘python’:

c:program filespython36libsite-packagespandasioparsers.py in
init(self, src, **kwds) 1682 kwds[‘allow_leading_cols’] = self.index_col is not False 1683
-> 1684 self._reader = parsers.TextReader(src, **kwds) 1685 1686 # XXX

pandas_libsparsers.pyx in
pandas._libs.parsers.TextReader.cinit()

pandas_libsparsers.pyx in
pandas._libs.parsers.TextReader._setup_parser_source()

OSError: Initializing from file failed

Other files in the same folder that are XLS files can be accessed without an issue.

When using the Python library like so:

import csv
file = csv.reader(open(r'pathtofile')) 

for row in file:
    print(row)
    break

df = pd.read_csv(file, sep=';')

the file is being loaded and the first line is printed. However I get:

ValueError: Invalid file path or buffer object type:

Probably because I can’t use read_csv this way…

How to get the first pandas function to work? The csv does not contain any special characters except German ones. The filesize is 10MB.

Asked By: rStorms

||

Answers:

I assume that your csv file is in the same place (root).
If you just want to the csv file to be read, and get the result that will show as text in your console, just do this

import csv
with open('your_file.csv', 'r') as csvFile:
    reader = csv.reader(csvFile)
    for row in reader:
        print(row)
csvFile.close()

note: the code is for Python 3, if you use Python 2 the print syntax use without brackets. Hope this will help you

Answered By: Ranee
import pandas as pd
pd.read_csv("your_file.txt", engine='python')

Try this. It totally worked for me.

Answered By: Dane Lee

You can try using os.path.join() to build your path:

import os

rpath = os.path.join('U:','folder','Input','test.csv')
df = pd.read_csv(rpath, sep=';')

To traverse path based on your parent directory, you can use:

os.path.pardir
Answered By: Varun kumar

I ran into a similar problem. It turned out the CSV I had downloaded had no permissions at all. The error message from pandas did not point this out, making it hard to debug.

Check that your file have read permissions

Answered By: Breck

I had the same issue, you should check your permissions.

After chmod 644 file.csv it worked well.

Answered By: Kornel Dylski

I find the same problem under Win10 OS when I try to read a csv file which name is in Chinese. Then there’s no problem any more after I rename my file into EN. Maybe you should ensure your full csv file path in EN.

OS:Windows 10; Python version:3.6.5; Ipython:7.0.1; Pandas:0.23.0

First, when use

import pandas as pd
answer_df = pd.read_csv('./答案.csv')

my ipython notebook raise ‘OSError: Initializing from file failed’.

Then I rename my file into answers.csv

import pandas as pd
answer_df = pd.read_csv('./answers.csv')

everythings’ OK.

May to help you.

Answered By: Sewen Scorpius

Same problem different solution here.

I had previously tried to load my file in excel, and excel had crashed but must have retained some file lock because once I force-quit excel, it loaded as expected.

Answered By: gkennos

Ran into the same issue on windows.
Tried to use to the solution provided by Dan Lee, but was getting abnormal results with rows and columns. I am not sure whether this was because of the Japanese characters in my csv files or not but by clearly defining the encoding format solved the problem for me.

import pandas as pd
pd.read_csv("your_file.txt", engine='python', encoding = "utf-8-sig")
Answered By: kunal Vyas

pandas read_csv OSError: Initializing from file failed

We could try chmod 600 file.csv.

Answered By: icanxy

just change the permessions of csv file, It would work

chmod 750 filename.csv (in command line)

or

!chmod 750 filename.csv (in jupyter notebook)

Same problem when I was trying to load files with Japanese filenames.

import pandas as pd
result = pd.read_csv('./result/けっこう.csv')

OSError: Initializing from file failed'

Then I added an argument engine="python".

result = pd.read_csv('./result/けっこう.csv', engine="python")

It worked for me.

Answered By: QaraQoyunlu

I had the same issue and since I have just installed another package, I realized that the problem could be due to that recent installation which may made some modifications to the pandas package.
I tried to remove pandas and reinstall it and it worked perfectly as before.

First remove pandas:

conda remove pandas

Then reinstall it using:

conda install -c anaconda pandas 
Answered By: Natheer Alabsi

may be the cause of the problem is that the version of pandas is too low, update the pandas to 1.0.1, it work. use the command as follows:

pip install -U pandas
Answered By: chenwei

I was facing the same issue from hours and this time I tried to the zip file in reference from where I downloaded the Dataset and it works-

df=pd.read_csv(r'C:UsersADMINnews.csv')-Initializing from file failed
df=pd.read_csv(r'C:UsersADMINnews.zip')-Worked nicely!!

Answered By: vikrant

In my case, I was entering incorrectly the path.

I was working with a dataset downloaded from Kaggle as a zip. The structure of downloads was: main.zip/subfiles.zip. I unzipped the main but the solution was to unzip the subfiles.zip and then my wanted file was within the subfiles.zip.

So the path would be main/subfiles/wantedfile.csv and this was my simple fix.

Answered By: Nicole Douglas

If your are facing this problem in jupyter notebook, then have you to upload the csv file in notebook and then check if your problem is solved or not. i hope this will work.

Answered By: Jai Ranawat 25

OSError: Initializing from file failed

By this error Python indicates the file is not readable- either, file permission, file error, incorrect path. etc.

My code giving this error :

DataFrame=pd.read_csv("C:\Users\arindam\Documents\#Books & Docs\#ML-DATA\train.csv")

Fixed by this :

DataFrame=pd.read_csv("C:\Users\arindam\Documents\train.csv")

Make sure the path is readable by python.

Answered By: Arindam Sau
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.