python - Permission denied when pandas dataframe to tempfile csv -
i'm trying store pandas dataframe tempfile in csv format (in windows), being hit by:
[errno 13] permission denied: 'c:\users\username\appdata\local\temp\tmpweymbkye'
import tempfile import pandas tempfile.namedtemporaryfile() temp: df.to_csv(temp.name)
where df dataframe. i've tried changing temp directory 1 sure have write permissions:
tempfile.tempdir='d:/username/temp/'
this gives me same error message
edit:
the tempfile appears locked editing when change loop to:
with tempfile.namedtemporaryfile() temp: df.to_csv(temp.name + '.csv')
i can write file in temp directory, not automatically deleted @ end of loop, no longer temp file.
however, if change code to:
with tempfile.namedtemporaryfile(suffix='.csv') temp: training_data.to_csv(temp.name)
i same error message before. file not open anywhere else.
check permissions and, according this post, can run program administrator right click , run administrator.
we can use to_csv command export dataframe in csv format. note code below default save data current working directory. can save different folder adding foldername , slash file
verticalstack.to_csv('foldername/out.csv').
check out working directory make sure csv wrote out properly, , can open it! if want, try bring python make sure imports properly.
newoutput = pd.read_csv('out.csv', keep_default_na=false, na_values=[""])
unlike temporaryfile()
, user of mkstemp()
responsible deleting temporary file when done it.
with use of function may introduce security hole in program. time around doing file name returns, else may have beaten punch. mktemp()
usage can replaced namedtemporaryfile()
, passing delete=false
paramete.
after export csv
can close file temp.close()
.
with tempfile.namedtemporaryfile(delete=false) temp: df.to_csv(temp.name + '.csv') temp.close()
Comments
Post a Comment