AttributeError:' NoneType'对象没有以

时间:2015-12-05 16:52:09

标签: python attributeerror

我要运行以下文件,但是我收到了错误消息。我不知道如何解决这个问题。请指导我。谢谢所有人。

我收到了这个错误:

Traceback(most recent call last)    :
  File "scripts/generate_simulated_pair.py", line 55, in <module>
    pair, d.strftime("%Y%m%d")
  File "home/farshad/venv/qsforex/lib/python2.7/posixpath.py", line 77, in join
    elif path == '' or path.endswith('/'):
AttributeError: 'NoneType' object has no attribute 'endswith'

错误引用的文件的一部分:

# Loop over every day in the month and create a CSV file
    # for each day, e.g. "GBPUSD_20150101.csv"
for d in days:
    print(d.day)
    current_time = current_time.replace(day=d.day)
    outfile = open(
        os.path.join(
            settings.CSV_DATA_DIR, 
            "%s_%s.csv" % (
                pair, d.strftime("%Y%m%d")
            )
        ), 
        "w")
    outfile.write("Time,Ask,Bid,AskVolume,BidVolume\n")>

我的setting.py:

from decimal import Decimal
import os


ENVIRONMENTS = { 
"streaming": {
    "real": "stream-fxtrade.oanda.com",
    "practice": "stream-fxpractice.oanda.com",
    "sandbox": "stream-sandbox.oanda.com"
},
"api": {
    "real": "api-fxtrade.oanda.com",
    "practice": "api-fxpractice.oanda.com",
    "sandbox": "api-sandbox.oanda.com"
}
}

CSV_DATA_DIR = os.environ.get('desktop/trading python files/trading      system/qsforex-backtesting-data', None)
OUTPUT_RESULTS_DIR = os.environ.get('desktop/trading python files/trading  system/qsforex-backtesting-results', None)

DOMAIN = "practice"
STREAM_DOMAIN = ENVIRONMENTS["streaming"][DOMAIN]
API_DOMAIN = ENVIRONMENTS["api"][DOMAIN]
ACCESS_TOKEN = os.environ.get('OANDA_API_ACCESS_TOKEN', None)
ACCOUNT_ID = os.environ.get('OANDA_API_ACCOUNT_ID', None)

BASE_CURRENCY = "USD"
EQUITY = Decimal("1000.00")

1 个答案:

答案 0 :(得分:0)

抛出异常是因为settings.CSV_DATA_DIRos.path.join()的第一个参数)设置为None

设置为None,因为环境变量desktop/trading python files/trading system/qsforex-backtesting-data不存在:

CSV_DATA_DIR = os.environ.get('desktop/trading python files/trading      system/qsforex-backtesting-data', None)

os.environ对象是一个映射,如果第一个参数(一个键)不存在,.get()方法返回第二个参数(这里是None)。