Python IO错误:csv文件不存在,尽管它存在于指定的给定位置

Python IO错误:csv文件不存在,尽管它存在于指定的给定位置,python,python-2.7,numpy,pandas,image-recognition,Python,Python 2.7,Numpy,Pandas,Image Recognition,Spyder中的编译时错误 import pandas as pd import os import time from datetime import datetime path = "C:\WinPython-32bit-2.7.9.5\python- 2.7.9\Lib\idlelib\MuditPracticals\intraQuarter\intraQuarter" def Key_Stats(gather="Total Debt/Equity (mrq)"): stats

Spyder中的编译时错误

import pandas as pd
import os
import time
from datetime import datetime

path = "C:\WinPython-32bit-2.7.9.5\python- 2.7.9\Lib\idlelib\MuditPracticals\intraQuarter\intraQuarter"

def Key_Stats(gather="Total Debt/Equity (mrq)"):
    statspath = path+'/_KeyStats'
    stock_list = [x[0] for x in os.walk(statspath)]
df = pd.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio','Price','SP500'])

sp500_df = pd.DataFrame.from_csv("YAHOO-INDEX_GSPC.csv")

for each_dir in stock_list[1:25]:
    each_file = os.listdir(each_dir)
    ticker = each_dir.split("\\")[3]
    if len(each_file) > 0:
        for file in each_file:
            date_stamp = datetime.strptime(file, '%Y%m%d%H%M%S.html')
            unix_time = time.mktime(date_stamp.timetuple())
            full_file_path = each_dir+'/'+file
            source = open(full_file_path,'r').read()
            try:
                value = float(source.split(gather+':</td><td class="yfnc_tabledata1">')[1].split('</td>')[0])

                try:
                    sp500_date = datetime.fromtimestamp(unix_time).strftime('%Y-%m-%d')
                    row = sp500_df[(sp500_df.index == sp500_date)]
                    sp500_value = float(row["Adjusted Close"])
                except:
                    sp500_date = datetime.fromtimestamp(unix_time-259200).strftime('%Y-%m-%d')
                    row = sp500_df[(sp500_df.index == sp500_date)]
                    sp500_value = float(row["Adjusted Close"])


                stock_price = float(source.split('</small><big><b>')[1].split('</b></big>')[0])
                #print("stock_price:",stock_price,"ticker:", ticker)



                df = df.append({'Date':date_stamp,
                                'Unix':unix_time,
                                'Ticker':ticker,
                                'DE Ratio':value,
                                'Price':stock_price,
                                'SP500':sp500_value}, ignore_index = True)
            except Exception as e:
                print "hello"

save = gather.replace(' ','').replace(')','').replace('(','').replace('/','')+('.csv')
print(save)
df.to_csv(save)


Key_Stats()
虽然文件存在于该位置,但给出IO错误

IO错误发生在编译时 以及为什么在其他空闲pandas模块中找不到,但在Spyder中没有pandas错误

到.csv文件的路径是相对的。如果文件不在当前工作目录中,python将找不到它

虽然文件存在于该位置。。。这就是相对路径的问题:那个位置是什么

以下是应该解决此问题的先前答案:

您的路径看起来无效。您需要避开反斜杠,或使用原始字符串,前缀为r so path=rC:\Win。。。。。。它根本不起作用。现在,它还显示了未找到csv文件的IO错误。
    File "<ipython-input-1-dfafbc7450e8>", line 1, in <module>
       runfile('C:/WinPython-32bit-2.7.9.5/python-   2.7.9/Lib/idlelib/MuditPracticals/data_organisation1.py', wdir='C:/WinPython-32bit-2.7.9.5/python-2.7.9/Lib/idlelib/MuditPracticals')

 File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile
    execfile(filename, namespace)

 File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)


File "C:/WinPython-32bit-2.7.9.5/python-2.7.9/Lib/idlelib/MuditPracticals/data_organisation1.py", line 56, in <module>
    Key_Stats()

 File "C:/WinPython-32bit-2.7.9.5/python-2.7.9/Lib/idlelib/MuditPracticals/data_organisation1.py", line 13, in Key_Stats
    sp500_df = pd.DataFrame.from_csv("YAHOO-INDEX_GSPC.csv")

 File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\pandas\core\frame.py", line 1036, in from_csv
    infer_datetime_format=infer_datetime_format)


File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\pandas\io\parsers.py", line 474, in parser_f
    return _read(filepath_or_buffer, kwds)


File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\pandas\io\parsers.py", line 250, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)

File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\pandas\io\parsers.py", line 566, in __init__
    self._make_engine(self.engine)

File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\pandas\io\parsers.py", line 705, in _make_engine
    ``self._engine = CParserWrapper(self.f, **self.options)


File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\pandas\io\parsers.py", line 1072, in __init__
    self._reader = _parser.TextReader(src, **kwds)

File "pandas\parser.pyx", line 350, in pandas.parser.TextReader.__cinit__ (pandas\parser.c:3160)

File "pandas\parser.pyx", line 594, in pandas.parser.TextReader._setup_parser_source (pandas\parser.c:5905)

IOError: File YAHOO-INDEX_GSPC.csv does not exist