为python脚本创建BAT文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4571244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:22:40  来源:igfitidea点击:

Creating a BAT file for python script

pythonbatch-file

提问by Josh

How can I create a simple BAT file that will run my python script located at C:\somescript.py?

如何创建一个简单的 BAT 文件来运行位于 C:\somescript.py 的 python 脚本?

采纳答案by MK.

c:\python27\python.exe c:\somescript.py %*

回答by Hugh Bothwell

Open a command line (? Win+R, cmd, ? Enter) and type python -V, ? Enter.

打开命令行(? Win+ R, cmd, ? Enter)并键入python -V, ? Enter

You should get a response back, something like Python 2.7.1.

你应该得到回复,比如Python 2.7.1

If you do not, you may not have Python installed. Fix this first.

如果没有,您可能没有安装 Python。先解决这个问题。

Once you have Python, your batch file should look like

一旦你有了 Python,你的批处理文件应该看起来像

@echo off
python c:\somescript.py %*
pause

This will keep the command window open after the script finishes, so you can see any errors or messages. Once you are happy with it you can remove the 'pause' line and the command window will close automatically when finished.

这将在脚本完成后保持命令窗口打开,因此您可以看到任何错误或消息。一旦您对它感到满意,您可以删除“暂停”行,完成后命令窗口将自动关闭。

回答by Pheng-Khai Tan

Just simply open a batch file that contains this two lines in the same folder of your python script:

只需在 python 脚本的同一文件夹中打开一个包含这两行的批处理文件:

somescript.py
pause

回答by npocmaka

Here's how you can put both batch code and the python one in single file:

以下是将批处理代码和 python 代码放在单个文件中的方法:

0<0# : ^
''' 
@echo off
echo batch code
python "%~f0" %*
exit /b 0
'''

print("python code")

the '''respectively starts and ends python multi line comments.

'''分别开始和结束蟒蛇多行注释。

0<0# : ^is more interesting - due to redirection priority in batch it will be interpreted like :0<0# ^by the batch script which is a label which execution will be not displayed on the screen. The caret at the end will escape the new line and second line will be attached to the first line.For python it will be 0<0statement and a start of inline comment.

0<0# : ^更有趣 - 由于批处理中的重定向优先级,它将被解释:0<0# ^为批处理脚本,这是一个不会在屏幕上显示执行的标签。最后的插入符号将转义新行,第二行将附加到第一行。对于python,它将是0<0语句和内联注释的开始。

The credit goes to siberia-man

归功于西伯利亚人

回答by Chris Farr

If you've added Python to your PATH then you can also simply run it like this.

如果您已将 Python 添加到您的 PATH 中,那么您也可以像这样简单地运行它。

python somescript.py

回答by Pierone

--- xxx.bat ---

--- xxx.bat ---

@echo off
set NAME1="Marc"
set NAME2="Travis"
py -u "CheckFile.py" %NAME1% %NAME2%
echo %ERRORLEVEL%
pause

--- yyy.py ---

--- yyy.py ---

import sys
import os
def names(f1,f2):

    print (f1)
    print (f2)
    res= True
    if f1 == "Travis":
         res= False
    return res

if __name__ == "__main__":
     a = sys.argv[1]
     b = sys.argv[2]
     c = names(a, b) 
     if c:
        sys.exit(1)
    else:
        sys.exit(0)        

回答by Mike T

If this is a BAT file in a different directory than the current directory, you may see an error like "python: can't open file 'somescript.py': [Errno 2] No such file or directory". This can be fixed by specifying an absolute path to the BAT file using %~dp0(the drive letter and path of that batch file).

如果这是与当前目录不同的目录中的 BAT 文件,您可能会看到类似“python: 无法打开文件 'somescript.py': [Errno 2] 没有这样的文件或目录”这样的错误。这可以通过使用%~dp0(该批处理文件驱动器号和路径)指定 BAT 文件的绝对路径来解决。

@echo off
python %~dp0\somescript.py %*

(This way you can ignore the c:\or whatever, because perhaps you may want to move this script)

(这样你就可以忽略c:\或者别的什么,因为也许你可能想要移动这个脚本)

回答by Hans Ginzel

You can use python code directly in batch file, https://gist.github.com/jadient/9849314.

您可以直接在批处理文件中使用 python 代码, https://gist.github.com/jadient/9849314

@echo off & python -x "%~f0" %* & goto :eof
import sys
print("Hello World!")

See explanation, Python command line -x option.

请参阅说明,Python 命令行 -x 选项

回答by nxttym

start xxx.py

开始 xxx.py

You can use this for some other file types.

您可以将其用于其他一些文件类型。

回答by Mandar Deshkar

ECHO OFF
set SCRIPT_DRIVE = %1
set SCRIPT_DIRECTORY = %2
%SCRIPT_DRIVE%
cd %SCRIPT_DRIVE%%SCRIPT_DIRECTORY%
python yourscript.py`