Permission denied when pandas dataframe to tempfile csv

后端 未结 6 1997

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\\Loca

相关标签:
6条回答
  • 2021-02-20 02:40

    Sometimes,you need check the file path that if you have right permission to read and write file. Especially when you use relative path.

    xxx.to_csv('%s/file.csv'%(file_path), index = False)
    
    0 讨论(0)
  • 2021-02-20 02:43

    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)
    
    0 讨论(0)
  • 2021-02-20 02:47

    Sometimes, it gives that error simply because there is another file with the same name and it has no permission to delete the earlier file and replace it with the new file.

    1. So either name the file differently while saving it, or
    2. If you are working on Jupyter Notebook or a other similar environment, delete the file after executing the cell that reads it into memory. So that when you execute the cell which writes it to the machine, there is no other file that exists with that name.
    0 讨论(0)
  • 2021-02-20 02:49

    As per my knowledge, this error pops up when one attempt to save the file that have been saved already and currently open in the background.

    You may try closing those files first and then rerun the code.

    0 讨论(0)
  • 2021-02-20 03:00

    Check your permissions and, according to this post, you can run your program as an administrator by right click and run as administrator.

    We can use the to_csv command to do export a DataFrame in CSV format. Note that the code below will by default save the data into the current working directory. We can save it to a different folder by adding the foldername and a slash to the file

    verticalStack.to_csv('foldername/out.csv').
    

    Check out your working directory to make sure the CSV wrote out properly, and that you can open it! If you want, try to bring it back into python to make sure it imports properly.

    newOutput = pd.read_csv('out.csv', keep_default_na=False, na_values=[""])
    

    ref

    Unlike TemporaryFile(), the user of mkstemp() is responsible for deleting the temporary file when done with it.

    With the use of this function may introduce a security hole in your program. By the time you get around to doing anything with the file name it returns, someone else may have beaten you to the punch. mktemp() usage can be replaced easily with NamedTemporaryFile(), passing it the delete=False paramete.

    Read more.

    After export to CSV you can close your file with temp.close().

    with tempfile.NamedTemporaryFile(delete=False) as temp:
        df.to_csv(temp.name + '.csv')
        temp.close()
    
    0 讨论(0)
  • 2021-02-20 03:00

    Just give a valid path and a file name

    e.g:

    final_df.to_csv('D:\Study\Data Science\data sets\MNIST\sample.csv')
    
    0 讨论(0)
提交回复
热议问题