windows batch脚本中嵌入python程序

本文介绍了如何在Windows批处理脚本(batch script)中嵌入Python程序。通过使用特殊语法,使得批处理解析器不会执行Python代码,而Python解释器能够正确识别并运行这部分代码。关键在于利用批处理和Python都认可的标签(label)机制,避免批处理解释器打印或执行Python代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1>2# : ^
'''
@echo off
python "%~f0"
exit /b
rem ^
'''
import os
print "This is Python code"


具体解释参考以下英文:

The batch code is in a multiline string ''' so this is invisible for python.
The batch parser doesn't see the python code, as it exits before.

The first line is the key.
It is valid for batch as also for python!
In python it's only a senseless compare 1>2 without output, the rest of the line is a comment by the #.

For batch 1>2# is a redirection of stream 1 to the file 2#.
The command is a colon : this indicates a label and labeled lines are never printed.
Then the last caret simply append the next line to the label line, so batch doesn't see the ''' line.


此段英文解释摘自:https://stackoverflow.com/questions/17467441/how-to-embed-python-code-in-batch-script


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值