Python 类型错误:join() 参数必须是 str 或字节,而不是“NoneType”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44613507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: join() argument must be str or bytes, not 'NoneType'
提问by riaan george
I keep on getting the error below, can somebody please tel me what I'm doing wrong?
我不断收到以下错误,有人可以告诉我我做错了什么吗?
Traceback (most recent call last):
File "generate_simulated_pair.py", line 50, in <module>
outfile = open(os.path.join(settings.CSV_DATA_DIR ,"%s_%s.csv"%(pair, d.strftime ("%Y%m%d")
File "C:\Program Files\Python35\lib\ntpath.py", line 113, in join
genericpath._check_arg_types('join', path, *paths)
File "C:\Program Files\Python35\lib\genericpath.py", line 143, in _check_arg_types
(funcname, s.__class__.__name__)) from None
TypeError: join() argument must be str or bytes, not 'NoneType'
Please see the see below, this is for creating a CSV file from oanda for backtesting
请参阅下面的内容,这是用于从 oanda 创建 CSV 文件以进行回测
from __future__ import print_function
import calendar
import copy
import datetime
import os, os.path
import sys
import numpy as np
import pandas as pd
import pandas_datareader
from pandas_datareader import data, wb
from qsforex import settings
def month_weekdays(year_int, month_int):
"""
Produces a list of datetime.date objects representing the
weekdays in a particular month, given a year.
"""
cal = calendar.Calendar()
return [d for d in cal.itermonthdates(year_int, month_int)if d.weekday() < 5 and d.year == year_int]
if __name__ == "__main__":
try:
pair = sys.argv[1]
except IndexError:
print("You need to enter a currency pair, e.g. GBPUSD, as a command line parameter.")
else:
np.random.seed(42) # Fix the randomness
S0 = 1.5000
spread = 0.002
mu_dt = 1400 # Milliseconds
sigma_dt = 100 # Millseconds
ask = copy.deepcopy(S0) + spread / 2.0
bid = copy.deepcopy(S0) - spread / 2.0
days = month_weekdays(2014, 1) # January 2014
current_time = datetime.datetime(
days[0].year, days[0].month, days[0].day, 0, 0, 0,
)
# 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")
)
),
"wb")
outfile.write("Time,Ask,Bid,AskVolume,BidVolume\n")
# Create the random walk for the bid/ask prices
# with fixed spread between them
while True:
dt = abs(np.random.normal(mu_dt, sigma_dt))
current_time += datetime.timedelta(0, 0, 0, dt)
if current_time.day != d.day:
outfile.close()
break
else:
W = np.random.standard_normal() * dt / 1000.0 / 86400.0
ask += W
bid += W
ask_volume = 1.0 + np.random.uniform(0.0, 2.0)
bid_volume = 1.0 + np.random.uniform(0.0, 2.0)
line = "%s,%s,%s,%s,%s\n" % (
current_time.strftime("%d.%m.%Y %H:%M:%S.%f")[:-3],
"%0.5f" % ask, "%0.5f" % bid,
"%0.2f00" % ask_volume, "%0.2f00" % bid_volume
)
outfile.write(line)
回答by Martijn Pieters
settings.CSV_DATA_DIR
is None, so os.path.join(settings.CSV_DATA_DIR, ...)
fails.
settings.CSV_DATA_DIR
是无,所以os.path.join(settings.CSV_DATA_DIR, ...)
失败。
For future reference, do look at the traceback and follow the stack back to your own code; your traceback starts with:
为了将来参考,请查看回溯并按照堆栈返回您自己的代码;您的回溯始于:
File "generate_simulated_pair.py", line 50, in <module>
outfile = open(os.path.join(settings.CSV_DATA_DIR ,"%s_%s.csv"%(pair, d.strftime ("%Y%m%d")
The exception at the bottom tells you:
底部的异常告诉您:
join() argument must be str or bytes, not 'NoneType'
so one of the arguments to os.path.join()
is a None
value. Of the two you pass in, the second is a definitely a string, leaving you with only one option: the otherargument mustbe None
.
所以参数之一os.path.join()
是一个None
值。在您传入的两个参数中,第二个绝对是一个字符串,只剩下一个选项:另一个参数必须是None
.