【问题标题】:Python os.path.join - TypeError: object of type 'NoneType' has no len()Python os.path.join - TypeError:“NoneType”类型的对象没有 len()
【发布时间】:2016-06-06 02:40:28
【问题描述】:

我对 python 比较陌生,我正在尝试从我的 Windows 命令提示符运行一个开源项目。但是,我收到以下错误:

C:\Users\Joycelin>cd C:\Users\Joycelin\Anaconda2\Lib\site-packages\algofx\script
s

C:\Users\Joycelin\Anaconda2\Lib\site-packages\algofx\scripts>python randomwalk.p
y GBPUSD
1
Traceback (most recent call last):
  File "randomwalk.py", line 62, in <module>
    pair, d.strftime("%Y%m%d")
  File "C:\Users\Joycelin\Anaconda2\lib\ntpath.py", line 65, in join
    result_drive, result_path = splitdrive(path)
  File "C:\Users\Joycelin\Anaconda2\lib\ntpath.py", line 115, in splitdrive
    if len(p) > 1:
TypeError: object of type 'NoneType' has no len()

这是发生错误的脚本 (randomwalk.py) 的一部分:

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(2016, 2)  # 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")
                    )
                ), 
            "w")
            outfile.write("Time,Ask,Bid,AskVolume,BidVolume\n")

我试图确定引发错误的 'None' 值在哪里,但仍然无法确定它是来自 settings.CSV_DATA_DIR 还是 d.strftime("%Y%m%d")(或者可能是其他原因?)。

这是脚本settings.py的一部分

CSV_DATA_DIR = os.environ.get("//Users/Joycelin/csv_data")
OUTPUT_RESULTS_DIR = os.environ.get("//Users/Joycelin/Anaconda2/Lib/site-packages/algofx/output_results")

【问题讨论】:

  • 我很确定您没有使用这些名称的环境变量 - 您确定要在其中调用 os.environ.get 吗?
  • settings.py 是从哪里来的?是在示例中还是您编写的? os.environ.get 获取环境变量,但那是一条路径……CSV_DATA_DIR = "//Users/Joycelin/csv_data" 等等……应该在那里。

标签: python typeerror export-to-csv nonetype


【解决方案1】:

你的 settings.py 应该包含这样的内容:

CSV_DATA_DIR = 'C:\\Users\\Joycelin\\csv_data'
OUTPUT_RESULTS_DIR = 'C:\\Users\\Joycelin\\output_results'

我认为您不想将输出放在 Anaconda 安装的目录之一中。

【讨论】:

    猜你喜欢
    • 2021-03-03
    • 2015-07-30
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 2021-10-21
    • 2018-06-11
    相关资源
    最近更新 更多