'SQL Server stored procedure Python passed file path

I am trying to pass a file directory to a Python script in order to process the contents. Here is the EXEC statement and below my code is the error. It is supposed open the file and do some processing.

EXEC ScrapeData 'F:\FinancialResearch\SEC\myEdgar\sec-edgar-filings\WIRE\10-Q\0001753926-20-000110\full-submission.txt'

DROP PROCEDURE IF EXISTS ScrapeData;
GO

CREATE PROCEDURE [dbo].[ScrapeData] 
    (@filepath varchar(300))
AS
BEGIN
    EXEC sp_execute_external_script
            @language = N'Python',
            @script = N'
import html2text
from bs4 import BeautifulSoup
import pyodbc
import re
import requests
import pandas as pd

with open(@filepath) as f:
        contents = f.read()
        soup = BeautifulSoup(contents, ''lxml'')
.....

Error:

Msg 39004, Level 16, State 20, Line 0
A 'Python' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004.

Msg 39019, Level 16, State 2, Line 0
An external script error occurred:

Error in execution. Check the output for more information.

Traceback (most recent call last):
File "", line 3, in
File "E:\ProgramData\REAPER\Temp-PY\Appcontainer1\DE944528-CBDA-4AFD-B50F-32AD2CFD35B7\sqlindb_0.py", line 45
with open(@filepath) as f:
^
SyntaxError: invalid syntax

SqlSatelliteCall error: Error in execution. Check the output for more information.

STDOUT message(s) from external script:

SqlSatelliteCall function failed. Please see the console output for more information.



Solution 1:[1]

...for a file c:\temp\test.txt..

declare @myfile varchar(100) = 'c:\temp\test.txt';

EXEC sp_execute_external_script @language = N'Python', @script = N'
print(filename)
print(xyz)
print(paramZ)
num_lines = sum(1 for line in open(filename))
print("------")
print(num_lines)
',
@params= N'@filename varchar(100), @paramZ int, @xyz varchar(10)', @filename = @myfile, @xyz='abcd', @paramZ = 100;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lptr