Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas DataFrame.to_csv raising IOError: No such file or directory

Hi: I am trying to use the Pandas DataFrame.to_csv method to save a dataframe to a csv file:

filename = './dir/name.csv'

df.to_csv(filename)

However I am getting the error:

IOError: [Errno 2] No such file or directory: './dir/name.csv'

Shouldn't the to_csv method be able to create the file if it doesn't exist? This is what I am intending for it to do.

like image 412
LoLa Avatar asked Nov 06 '17 18:11

LoLa


People also ask

Does To_csv create directory?

to_csv does create the file if it doesn't exist as you said, but it does not create directories that don't exist. Ensure that the subdirectory you are trying to save your file within has been created first. This can easily be wrapped up in a function if you need to do this frequently.

What does To_csv do in pandas?

Pandas DataFrame to_csv() function converts DataFrame into CSV data. We can pass a file object to write the CSV data into a file. Otherwise, the CSV data is returned in the string format.

Does To_csv overwrite file?

Does pandas To_csv overwrite? If the file already exists, it will be overwritten. If no path is given, then the Frame will be serialized into a string, and that string will be returned.


1 Answers

to_csv does create the file if it doesn't exist as you said, but it does not create directories that don't exist. Ensure that the subdirectory you are trying to save your file within has been created first.

I often do something like this in my work:

import os

outname = 'name.csv'

outdir = './dir'
if not os.path.exists(outdir):
    os.mkdir(outdir)

fullname = os.path.join(outdir, outname)    

df.to_csv(fullname)

This can easily be wrapped up in a function if you need to do this frequently.

like image 135
qRTPCR Avatar answered Sep 17 '22 17:09

qRTPCR