IOError:[Errno 13]权限被拒绝:导出到.csv

时间:2017-07-11 02:05:41

标签: python-2.7 csv

我在尝试将数据导出为.csv格式时遇到此错误。

我尝试以管理员身份运行该应用程序,但它无效。

请帮助新手!

以下是代码:

import pandas as pd
tickers = ['AAPL', 'MSFT', 'XOM', 'BP']
portfolio_selection = pd.DataFrame()
for t in tickers:
     portfolio_selection = wb.DataReader(tickers, 'google', start = '2005-1-1')['Close']
portfolio_selection
portfolio_selection.to_csv('C:\Users\PC\Documents\Lucas\Random_Folder')

这就是我所拥有的

--------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-6-0b1cec90f143> in <module>()
----> 1 portfolio_selection.to_csv('C:\Users\PC\Documents\Lucas\Random_Folder')

C:\Users\Pichau\Anaconda2\lib\site-packages\pandas\core\frame.pyc in to_csv(self, path_or_buf, sep, na_rep, float_format, columns, header, index, index_label, mode, encoding, compression, quoting, quotechar, line_terminator, chunksize, tupleize_cols, date_format, doublequote, escapechar, decimal)
   1411                                      doublequote=doublequote,
   1412                                      escapechar=escapechar, decimal=decimal)
-> 1413         formatter.save()
   1414 
   1415         if path_or_buf is None:

C:\Users\Pichau\Anaconda2\lib\site-packages\pandas\io\formats\format.pyc in save(self)
   1566             f, handles = _get_handle(self.path_or_buf, self.mode,
   1567                                      encoding=self.encoding,
-> 1568                                      compression=self.compression)
   1569             close = True
   1570 

C:\Users\Pichau\Anaconda2\lib\site-packages\pandas\io\common.pyc in _get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text)
    374         if compat.PY2:
    375             # Python 2
--> 376             f = open(path_or_buf, mode)
    377         elif encoding:
    378             # Python 3 and encoding

 IOError: [Errno 13] Permission denied: 'C:\Users\PC\Documents\Lucas\Random_Folder'

1 个答案:

答案 0 :(得分:1)

我不确定这个错误在Windows上会是什么样子但是我想它是因为你需要一个文件名。 (在Mac上,您的代码会抛出IsADirectoryError: [Errno 21] Is a directory: Random_Folder

这样的事情应该解决它:

portfolio_selection.to_csv('C:\Users\PC\Documents\Lucas\Random_Folder\portfolio_selection.csv')