You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地Azure Function输出路径配置问题及代码失效求助

Azure Functions中Scrapy路径配置引发Unicode转义错误的求助

我是Azure Functions新手,原本可以成功将Scrapy爬虫的输出保存到当前目录,但修改代码要把输出保存到指定文件夹时触发了Unicode转义错误。恢复之前验证有效的代码后,仍然出现相同错误,已经尝试了多种路径写法,希望能解决代码失效的问题,并得到正确的路径配置方案。

原工作代码

#Works NO issues
import azure.functions as func
import datetime
import logging
import subprocess
import os

app = func.FunctionApp()

@app.timer_trigger(schedule="0 * * * * *", arg_name="myTimer", run_on_startup=True, use_monitor=False)
def ScrapyTimerTrigger(myTimer: func.TimerRequest) -> None:
    
    if myTimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function executed.')

    week = 'week'  # Set your desired week
    year = '2023'  # Set your desired year
    game = 'hall-of-fame-weekend'  # Set your desired game
    local_file_name = f'{year}_{week}_{game}.json'
    #local_file_path = os.path.join(os.getcwd(), local_file_name)

    scrapy_command = [
        'scrapy', 'crawl', 'NFLWeatherData',
        '-a', f'Week={week}', '-a', f'Year={year}', '-a', f'Game={game}',
        '-o', local_file_name # local_file_path
    ]

    # Update this path to the correct directory
    process = subprocess.Popen(scrapy_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=r"N:\github\SportsData\NFLWeather\FunctionLocal\NFLWeather")
    stdout, stderr = process.communicate()

    if process.returncode != 0:
        logging.error(stderr)
    else:
        logging.info(stdout)

修改后的代码

import azure.functions as func
import datetime
import logging
import subprocess
import os

app = func.FunctionApp()

@app.timer_trigger(schedule="0 * * * * *", arg_name="myTimer", run_on_startup=True, 
use_monitor=False)
def ScrapyTimerTrigger(myTimer: func.TimerRequest) -> None:

    if myTimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function executed.')

    week = 'week'  # Set your desired week
    year = '2023'  # Set your desired year
    game = 'hall-of-fame-weekend'  # Set your desired game
    local_file_name = f'{year}_{week}_{game}.json'
    local_file_path = os.path.join(r"N:\\github\\SportsData\\NFLWeather\\Data_Gathering", year, local_file_name)

    # Create the directory if it does not exist
    directory_path = os.path.join(r"N:\\github\\SportsData\\NFLWeather\\Data_Gathering", year)
    os.makedirs(directory_path, exist_ok=True)

    scrapy_command = [
        'scrapy', 'crawl', 'NFLWeatherData',
        '-a', f'Week={week}', '-a', f'Year={year}', '-a', f'Game={game}',
        '-o', local_file_path
    ]

    # Update this path to the correct directory
    process = subprocess.Popen(scrapy_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=r"N:\github\SportsData\NFLWeather\FunctionLocal\NFLWeather")
    stdout, stderr = process.communicate()

    if process.returncode != 0:
        logging.error(stderr)
    else:
        logging.info(stdout)

错误日志

[2024-06-24T00:41:45.459Z] Received WorkerInitRequest, python version 3.11.9 | packaged by Anaconda, Inc. | (main, Apr 19 2024, 16:40:41) [MSC v.1916 64 bit (AMD64)], worker version 4.28.1, request ID 583ffc8a-a82c-49d4-b586-a0ded194964c. App Settings state: PYTHON_THREADPOOL_THREAD_COUNT: 1000 | PYTHON_ENABLE_WORKER_EXTENSIONS: False. 如需启用调试级别日志,请参考相关文档。
[2024-06-24T00:41:45.767Z] Received WorkerMetadataRequest, request ID 583ffc8a-a82c-49d4-b586-a0ded194964c, function_path: N:\github\SportsData\NFLWeather\FunctionLocal\NFLWeather\NFLWeatherFunctionApp\function_app.py
[2024-06-24T00:41:45.797Z] Worker failed to index functions
[2024-06-24T00:41:45.799Z] Result: Failure
Exception: SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 1269-1270: malformed \N character escape (function_app.py, line 43)
Stack:   File "M:\Microsoft\Azure Functions Core Tools\workers\python\3.11\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 413, in _handle__functions_metadata_request
    self.load_function_metadata(
  File "M:\Microsoft\Azure Functions Core Tools\workers\python\3.11\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 393, in load_function_metadata
    self.index_functions(function_path, function_app_directory)) \
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "M:\Microsoft\Azure Functions Core Tools\workers\python\3.11\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 765, in index_functions
    indexed_functions = loader.index_function_app(function_path)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "M:\Microsoft\Azure Functions Core Tools\workers\python\3.11\WINDOWS\X64\azure_functions_worker\utils\wrappers.py", line 44, in call
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "M:\Microsoft\Azure Functions Core Tools\workers\python\3.11\WINDOWS\X64\azure_functions_worker\loader.py", line 238, in index_function_app
    imported_module = importlib.import_module(module_name)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "M:\Miniconda3\envs\Azure\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 936, in exec_module
  File "<frozen importlib._bootstrap_external>", line 1074, in get_code
  File "<frozen importlib._bootstrap_external>", line 1004, in source_to_code
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed

尝试过的路径写法

# local_file_path = os.path.join("N:\\github\\SportsData\\NFLWeather\\Data_Gathering\\2024", local_file_name)
# local_file_path = os.path.join(r"N:\\github\\SportsData\\NFLWeather\\Data_Gathering\\2024", local_file_name)
# local_file_path = Path("N:/github/SportsData/NFLWeather/Data_Gathering/2024") / local_file_name
# local_file_path = Path(r"N:\github\SportsData\NFLWeather\Data_Gathering\2024") / local_file_name

内容的提问来源于stack exchange,提问作者Leo Torres

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 22:15:54