如何在python .bat文件中向函数添加参数



我有一个带有函数的文件和一个调用函数的文件。最后,运行.bat我不知道如何在调用.bat文件时添加参数。因此,参数被添加到函数中,如下所示。

file_with_func.py

def some_func(val):
    print(val)

run_bat.py

from bin.file_with_func import some_func
some_func(val)

myBat.bat

set basePath=%cd%
cd %~dp0
cd ..
python manage.py shell < bin/run_bat.py
cd %basePath%

现在我想像这样运行。bat。

bin>.myBat.bat "mystring"

或者在启动后,获取可供选择的选项,例如

bin>.myBat.bat
>>> Choose 1 or 2
>>> 1

然后函数返回"You chose 1"

您可以使用sys模块使用参数作为输入来运行它

在脚本中调用

python test.py var1 

在test.py的第一行中必须有

import sys
print(sys.argv[0]) # prints test
print(sys.argv[1]) # prints var1

In

bin>.myBat.bat "mystring"

"mystring"是参数。因此,我们需要告诉myBat.bat在哪里使用参数。

试题:myBat.bat

set basePath=%cd%
cd %~dp0
cd ..
python manage.py shell < bin/run_bat.py %1
cd %basePath%

%1表示唯一的参数。

最新更新