Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permission denied when pandas dataframe to tempfile csv

I'm trying to store a pandas dataframe to a tempfile in csv format (in windows), but am being hit by:

[Errno 13] Permission denied: 'C:\Users\Username\AppData\Local\Temp\tmpweymbkye'

import tempfile
import pandas

with tempfile.NamedTemporaryFile() as temp:
    df.to_csv(temp.name)

Where df is the dataframe. I've also tried changing the temp directory to one I am sure I have write permissions:

tempfile.tempdir='D:/Username/Temp/'

This gives me the same error message

Edit:

The tempfile appears to be locked for editing as when I change the loop to:

with tempfile.NamedTemporaryFile() as temp:
    df.to_csv(temp.name + '.csv')

I can write the file in the temp directory, but then it is not automatically deleted at the end of the loop, as it is no longer a temp file.

However, if I change the code to:

with tempfile.NamedTemporaryFile(suffix='.csv') as temp:
    training_data.to_csv(temp.name)

I get the same error message as before. The file is not open anywhere else.

like image 275
thebigspin Avatar asked Apr 07 '17 16:04

thebigspin


People also ask

How do I convert DF to pandas CSV?

By using pandas. DataFrame. to_csv() method you can write/save/export a pandas DataFrame to CSV File. By default to_csv() method export DataFrame to a CSV file with comma delimiter and row index as the first column.

Does pandas To_csv overwrite?

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.

Does PD read_csv return a DataFrame?

Read a CSV File In this case, the Pandas read_csv() function returns a new DataFrame with the data and labels from the file data. csv , which you specified with the first argument. This string can be any valid path, including URLs.

What does PD read_csv () do?

Read a comma-separated values (csv) file into DataFrame. Also supports optionally iterating or breaking of the file into chunks.


2 Answers

I encountered the same error message and the issue was resolved after adding "/df.csv" to file_path.

df.to_csv('C:/Users/../df.csv', index = False)
like image 168
hm6 Avatar answered Sep 22 '22 21:09

hm6


I encountered the same error. I simply had not yet saved my entire python file. Once I saved my python file in VS code as "insertyourfilenamehere".py to documents(which is in my path), I ran my code again and I was able to save my data frame as a csv file.

like image 41
dneal80 Avatar answered Sep 22 '22 21:09

dneal80