SQL Server 2022 和 Python 3.10 脚本错误

huangapple go评论69阅读模式
英文:

SQL Server 2022 and Python3.10 script error

问题

我需要从一个文件夹接收文件列表。
我运行了这个小脚本但是收到了错误:

@script = N'print(__import__("os").walk(r''E:\\Backups\\''))' ------------
STDOUT message(s)
from
external script : < generator object _walk at 0x0000015CD304DE00 >;

文件夹 E:\Backups\ 包含文件。
脚本中有什么不正确的地方?

英文:

I need to receive lists of files from a folder.
I run the small script but receive the error :

EXECUTE sp_execute_external_script @language = N &#39;Python&#39;, 
@script = N &#39;print(__import__(&quot;os&quot;).walk(r&#39;&#39;E:\\Backups\\&#39;&#39;))&#39; ------------
STDOUT message(s) 
from 
  external script : &lt; generator object _walk at 0x0000015CD304DE00 &gt;

The folder E:\Backups\ contains files.
What is incorrect in the script?

答案1

得分: 1

正如我在评论中提到的(@Siggemannen也提到了同样的问题),您收到的消息是因为您正在打印os.walk函数的输出,该函数返回一个生成器对象,该对象生成一个包含每次迭代的目录路径、子目录和文件名的元组。

要获取每个文件的绝对路径,您需要迭代os.path.join,然后在此之前迭代os.walk返回的文件名。在每次迭代中,将路径附加到文件列表中。

-- 创建一个表变量来存储输出
DECLARE @OutputTable TABLE (FilePath NVARCHAR(MAX));

-- 执行外部脚本
EXECUTE sp_execute_external_script
  @language = N'Python',
  @script = N'
import os
import pandas as pd

# 将文件存储在列表中
files = []
for root, dirs, filenames in os.walk(r"E:\\Backups\\"):
    for filename in filenames:
        files.append(os.path.join(root, filename))

# 返回文件列表
OutputDataSet = pd.DataFrame(files, columns=["FilePath"])
',
  @input_data_1 = N'',
  @output_data_1_name = N'OutputDataSet',
  @params = N'';

-- 将结果插入表变量中
INSERT INTO @OutputTable
SELECT FilePath FROM OutputDataSet;

-- 从表变量中选择结果
SELECT FilePath FROM @OutputTable;

(请注意,上述代码是一个SQL脚本,用于在数据库中执行Python代码以获取文件列表的绝对路径。)

英文:

As I mentioned in the comment (@Siggemannen also mentioned the same thing), the message you are getting is that you're printing the output of the os.walk function which returns a generator object that yields a tuple containing each iteration's directory path, subdirectories, and filenames.

To get the absolute path of each file, you need to iterate over the os.path.join and before that iterate over the filenames returned by os.walk.
In each iteration, paths are appended to the files list.

-- Create a table variable to store the output
DECLARE @OutputTable TABLE (FilePath NVARCHAR(MAX));

-- Execute the external script
EXECUTE sp_execute_external_script
  @language = N&#39;Python&#39;,
  @script = N&#39;
import os
import pandas as pd

# Store the files in a list
files = []
for root, dirs, filenames in os.walk(r&quot;E:\\Backups\\&quot;):
    for filename in filenames:
        files.append(os.path.join(root, filename))

# Return the list of files
OutputDataSet = pd.DataFrame(files, columns=[&quot;FilePath&quot;])
&#39;,
  @input_data_1 = N&#39;&#39;,
  @output_data_1_name = N&#39;OutputDataSet&#39;,
  @params = N&#39;&#39;;

-- Insert the result into the table variable
INSERT INTO @OutputTable
SELECT FilePath FROM OutputDataSet;

-- Select the result from the table variable
SELECT FilePath FROM @OutputTable;

答案2

得分: 1

你可以尝试使用 dm_os_enumerate_filesystem() dmv。

它从 SQL Server 2017 开始起效。

这个 dmv 接受两个参数:

  • 目录
  • 文件通配符

SQL

-- SQL Server 2017 开始
SELECT file_or_directory_name
   , level, is_directory, creation_time, size_in_bytes
FROM sys.dm_os_enumerate_filesystem
(
   N'C:\Program Files\Microsoft SQL Server\Client SDK\ODBC0\Tools\Binn'
   , N'*.*'
);

输出

文件或目录名称 级别 是否为目录 创建时间 字节大小
batchparser.dll 0 0 2022-10-08 10:33:18.0000000 190408
bcp.exe 0 0 2022-10-08 10:33:36.0000000 141224
Resources 0 1 2021-11-30 23:33:46.1047932 0
SQLCMD.EXE 0 0 2023-05-26 17:18:40.0000000 276376
xmlrw.dll 0 0 2019-09-24 19:27:18.0000000 347752
1033 1 1 2021-11-30 23:33:46.1053619 0
bcp.rll 2 0 2022-10-08 10:33:16.0000000 30664
license_SQLCMD.txt 2 0 2022-10-08 10:31:00.0000000 18102
SQLCMD.rll 2 0 2022-10-08 10:33:18.0000000 42912

<details>
<summary>英文:</summary>

You can try the `dm_os_enumerate_filesystem()` dmv.

It will work starting from SQL Server 2017 onwards.

The dmv accepts two parameters:

 - Directory 
 - Files wildcard

**SQL**

    -- SQL Server 2017 onwards
    SELECT file_or_directory_name
       , level, is_directory, creation_time, size_in_bytes
    FROM sys.dm_os_enumerate_filesystem
    (
       N&#39;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC0\Tools\Binn&#39;
       , N&#39;*.*&#39;
    );

**Output**

| file_or_directory_name | level | is_directory |        creation_time        | size_in_bytes |
|------------------------|-------|--------------|-----------------------------|---------------|
| batchparser.dll        |     0 |            0 | 2022-10-08 10:33:18.0000000 |        190408 |
| bcp.exe                |     0 |            0 | 2022-10-08 10:33:36.0000000 |        141224 |
| Resources              |     0 |            1 | 2021-11-30 23:33:46.1047932 |             0 |
| SQLCMD.EXE             |     0 |            0 | 2023-05-26 17:18:40.0000000 |        276376 |
| xmlrw.dll              |     0 |            0 | 2019-09-24 19:27:18.0000000 |        347752 |
| 1033                   |     1 |            1 | 2021-11-30 23:33:46.1053619 |             0 |
| bcp.rll                |     2 |            0 | 2022-10-08 10:33:16.0000000 |         30664 |
| license_SQLCMD.txt     |     2 |            0 | 2022-10-08 10:31:00.0000000 |         18102 |
| SQLCMD.rll             |     2 |            0 | 2022-10-08 10:33:18.0000000 |         42912 |



</details>



huangapple
  • 本文由 发表于 2023年6月27日 20:42:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定