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

2024-10-02 16:24:43 发布

您现在位置:Python中文网/ 问答频道 /正文

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()

Spyder中的编译时错误

^{pr2}$

虽然文件存在于该位置,但它给出了IO错误

IO ERROR occurs at compile time 为什么在另一个IDLE熊猫模块中找不到,而在Spyder中却没有pandas Error


Tags: csvpathimportdfdatetimedatetimevalue
1条回答
网友
1楼 · 发布于 2024-10-02 16:24:43

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

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

下面是一个可以解决问题的先前答案: Python ConfigParser cannot search .ini file correctly (Ubuntu 14, Python 3.4)

相关问题 更多 >