【问题标题】:Read/write same variable in batch and python scripts在批处理和 python 脚本中读/写相同的变量
【发布时间】:2016-06-22 14:24:53
【问题描述】:

我有 2 个脚本。一个是 .bat,另一个是 python。 python 脚本由 .bat 文件触发。执行时,首先我将使用命令行参数运行 .bat,然后我需要将参数读入 python 脚本。

我做错了什么?

我这样调用批处理脚本:

C:>main.bat c:\temp\text1.txt

main.bat:

@ECHO off
set var1=%~1
call python_script.bat 
echo "returned to main"
pause

python_script.bat:

python -x %0 %*

print var1        # Notworking

import sys
var1 = sys.argv  ############  Also not working

with open(var1, 'r+') as f:
content = f.read()
f.seek(0)
f.trunca........

【问题讨论】:

  • sys.argv 是一个列表。第一个元素是脚本本身的名称,第二个元素是第一个命令行参数。你为什么不简单地将你的 python 放在一个 script_name.py 文件中,然后从你的 .bat 文件中调用 python script_name.py 呢?相关问题:stackoverflow.com/questions/1013246/…
  • 我还有 2 个要调用的脚本......而且,我只需要 python 的命令行参数......和 ​​bat 脚本。第一个触发器将使用 .bat。这真的可能吗??
  • 但是,从 main.bat 到 python_script.bat 不传递 var1,是两个“实例”
  • 脚本不是使用同一个文件吗? (c:\temp\text1.txt) 在这种情况下,您不能简单地将命令行参数硬编码到 bat 文件中的 python 脚本,而不是尝试将 args 从 bat 传递到 py 吗?虽然我不在 Windows 上,所以我无法真正测试它。
  • No.. 实际上,(c:\temp\text1.txt) 就是一个例子。用户不知道这条路径。路径将与 .bat 文件一起作为命令行参数给出。并且python需要阅读它

标签: python batch-file cmd


【解决方案1】:

我不太了解 bat 和 windows,但是 windows 不支持从 bat 文件中调用带有命令行参数的 python 脚本吗?如果是这样,这样的事情不会起作用吗?

这适用于 Linux shell:

call_py.sh:

# do stuff with command line argument 1 here ($1) and pass it on to py
echo "I'm a shell script and i received this cmd line arg: " $1
# pass $1 on to python as a cmd line arg here
python some_script.py $1
echo "shell script still running after python script finished"

The other question I linked to 向我们展示了如何从 bat 调用 python(虽然我无法验证)。你不能像我在 call_py.sh 中那样在 py 脚本的名称后简单地添加 var1 吗?

# syntax might be wrong
start C:\python27\python.exe D:\py\some_script.py var1

some_script.py 然后接收 $1/var1 作为sys.argv[1]

some_script.py:

import sys

print "I'm a python script called " + sys.argv[0]
print "I received this cmd line arg from shell: " + sys.argv[1]

输出:

$ sh call_py.sh "VARIABLE_GIVEN_TO_SHELL_AND_PASSED_TO_PY"
I'm a shell script and i received this cmd line arg:  VARIABLE_GIVEN_TO_SHELL_AND_PASSED_TO_PY
I'm a python script called some_script.py
I received this cmd line arg from shell VARIABLE_GIVEN_TO_SHELL_AND_PASSED_TO_PY
shell script still running after python script finished

这行得通还是 Windows 比我想象的更奇怪? :)

更新:

我启动了旧的恶意软件磁铁并尝试从命令行 -> 批处理脚本 -> python 传递参数。我没有使用您的 python -x %0 %* 语法,它似乎允许在批处理脚本中运行 python 代码,而是简单地编写了一个单独的 .py 文件并从 .bat 文件中调用它。

这适用于 Windows 7:

call_py.bat:

@ECHO off

set var1=%~1
echo Bat script called with cmdline arg: "%var1%"
echo Passing cmdline arg to python script
call C:\Python27\python.exe C:\Users\bob\Desktop\print_arg1.py %var1%
echo Bat again - continuing...
pause

print_arg1.py:

import sys

try:
    print "Python says hi and received this cmdline arg: " + sys.argv[1]
except IndexError:
    print "Python says hi but didn't receive any cmdline args :("

输出:

C:\Users\bob\Desktop>call_py.bat I_AM_A_CMDLINE_ARG
Bat script called with cmdline arg: "I_AM_A_CMDLINE_ARG"
Passing cmdline arg to python script
Python says hi and received this cmdline arg: I_AM_A_CMDLINE_ARG
Bat again - continuing...
Press any key to continue . . .

【讨论】:

  • 我想将命令行参数写入一个新的 txt 文件。所以任何脚本都可以读取.txt。但是,在命令提示符下运行时,bat 文件无法在 C 盘中创建 .txt。但它在 D 驱动器中创建顺利..
  • 如果您可以确保不会同时从多个脚本写入和读取文件,那么写入文件是非常好的。此链接似乎显示了如何将参数从批处理传递到 python - 在这种情况下是在 for 循环中:dostips.com/forum/viewtopic.php?f=3&t=3431 也许这只是一个语法问题,您需要在 var1 前面加上一个或两个百分号。
猜你喜欢
  • 2011-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-30
相关资源
最近更新 更多