简体   繁体   中英

Export csv file fails with PyCharm

I have countered strange issue when using PyCharm, when trying to export csv's from pandas I get permission error although this is a new csv file and and doesn't open anywhere

my code:

import pandas as pd

df = pd.DataFrame()
df['A'] = ['21','22']
path = "temp.csv"
df.to_csv(path)

and the error I receive:

  File "C:/Users/MyName/myWorkingPath/temp.py", line 7, in <module>
    df.to_csv(path)
  File "C:\Users\MyName\Miniconda3\Lib\site-packages\pandas\core\generic.py", line 3466, in to_csv
    return DataFrameRenderer(formatter).to_csv(
  File "C:\Users\MyName\Miniconda3\Lib\site-packages\pandas\io\formats\format.py", line 1105, in to_csv
    csv_formatter.save()
  File "C:\Users\MyName\Miniconda3\Lib\site-packages\pandas\io\formats\csvs.py", line 237, in save
    with get_handle(
  File "C:\Users\MyName\Miniconda3\Lib\site-packages\pandas\io\common.py", line 702, in get_handle
    handle = open(
PermissionError: [Errno 13] Permission denied: 'temp.csv'

I tried the following:

  1. verified no open csv file - still occur
  2. reset my PyCharm IDE configuration - still occur
  3. Reinstalled and installed Pandas - still occur

4. Run from Spyder IDE - Worked!!!

Do anyone have any suggestion how solve this issue? I managed to work with csv's for long time but it suddenly happen

It's probably because your working directory is not the same between PyCharm and Spyder:

Try

import os
print(os.getcwd())

And use an absolute path:

df.to_csv('C:/Users/MyName/myWorkingPath/temp.csv')

Solved,

After restarting the computer I found that my Anti-Virus blocked python.exe from writing the file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM