【问题标题】:AttributeError: 'NoneType' object has no attribute endswithAttributeError:“NoneType”对象没有属性以结尾
【发布时间】:2016-03-10 13:16:29
【问题描述】:

我将运行以下文件,但出现错误。我不知道如何解决这个问题。请指导我。谢谢大家。

我收到了这个错误:

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

我的设置.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")

【问题讨论】:

  • settings.CSV_DATA_DIRNone

标签: python attributeerror


【解决方案1】:

抛出异常是因为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)。

【讨论】:

    猜你喜欢
    • 2019-01-01
    • 2021-12-26
    • 2019-07-23
    • 2018-05-13
    • 2020-09-07
    • 2017-05-03
    • 2023-03-16
    • 2018-07-14
    • 2013-06-16
    相关资源
    最近更新 更多