在windows批处理文件中的for循环中运行python脚本,并将一些参数传递给它们

2024-09-19 23:34:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试运行以下windows批处理文件:

@echo off

CALL workon my_env
CD C:\scripts

SET DataPath = C:\user\data
SET SavePath = C:\results

FOR %DataQuantity IN ( 100 75 60 50 40 33 25 10 ) DO (
    FOR %Subject IN ( 1 2 4 5 6 7 8 9 ) DO (
        python classifier.py DataQuantity Subject DataPath SavePath 
        python detector.py DataQuantity Subject DataPath SavePath
        python compiler.py DataQuantity Subject DataPath SavePath
    )
)

我想使用嵌套for循环创建三个具有不同参数的python脚本。每个python脚本都有四个参数,其中两个是预先设置的,两个来自for循环

但我得到了以下错误:

“此时数据量意外”


Tags: 文件inpyecho脚本for参数windows
2条回答
@echo off

CALL workon my_env
CD C:\scripts

SET DataPath=C:\user\data
SET SavePath=C:\results

FOR %%q IN ( 100 75 60 50 40 33 25 10 ) DO (
    FOR %%s IN ( 1 2 4 5 6 7 8 9 ) DO (
        python classifier.py %%q %%s %DataPath% %SavePath% 
        python detector.py %%q %%s %DataPath% %SavePath% 
        python compiler.py %%q %%s %DataPath% %SavePath% 
    )
)

修正了以下问题:

  1. “=”附近不应有任何空格
SET DataPath=C:\user\data
  1. 循环变量在批处理文件中使用时必须以“%”作为前缀,并且只能是一个字母
%%q
  1. 变量必须封装在“%”内
%DataPath%

您缺少百分比字符:

FOR %%B IN ( 100 75 60 50 40 33 25 10 ) DO (
    FOR %%S IN ( 1 2 4 5 6 7 8 9 ) DO (
        python classifier.py %%B %%S %DataPath% %SavePath%
        python detector.py %%B %%S %DataPath% %SavePath%
        python compiler.py %%B %%S %DataPath% %SavePath%
    )
)

在for use%%和to use setted变量use%VAR%

相关问题 更多 >