为Python脚本创建BAT文件
如何创建一个简单的 BAT 文件来运行位于 C:\somescript.py 的 python 脚本?
How can I create a simple BAT file that will run my python script located at C:\somescript.py?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
这是语法:
“python.exe path”“python script path”pause
基本上会发生什么,屏幕会出现几秒钟,然后消失,注意这两件事:
This is the syntax:
"python.exe path""python script path"pause
Basically what will be happening the screen will appear for seconds and then go off take care of these 2 things:
如果这是与当前目录不同的目录中的 BAT 文件,您可能会看到类似“python: can't open file 'somescript.py': [Errno 2] No such file or directory”的错误。可以通过使用
%~dp0
指定 BAT 文件的绝对路径(驱动器号和路径)来解决此问题批处理文件)。(这样您就可以忽略
c:\
或其他内容,因为也许您可能想要移动此脚本)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).(This way you can ignore the
c:\
or whatever, because perhaps you may want to move this script)使用任何文本编辑器并将以下代码保存为 runit.bat
现在将此文件放入 python 脚本所在的文件夹中。运行此文件并输入 python 脚本的文件名以使用批处理文件(cmd)运行 python 程序
参考:Narendra Dwivedi - 如何使用批处理文件运行 Python
Use any text editor and save the following code as runit.bat
Now place this file in the folder where python script is present. Run this file and enter python script's file name to run python program using batch file (cmd)
Reference : Narendra Dwivedi - How To Run Python Using Batch File
创建一个空文件并将其命名为“run.bat”
在我的例子中,我使用“py”,因为它更方便,请尝试:
Create an empty file and name it "run.bat"
In my case i use "py" because it's more convenient, try:
我这样做了并且有效:
我的项目位于 D: 中,我的批处理文件位于桌面中,如果您将其放在同一驱动器中,只需忽略第一行并在第二行中更改 de D 目录,
在第二行中更改文件的文件夹,将 将你的文件夹
中第三行的文件名更改为
D:
cd D:\python_proyects\example_folder\
python example_file.py
i did this and works:
i have my project in D: and my batch file is in the desktop, if u have it in the same drive just ignore the first line and change de D directory in the second line
in the second line change the folder of the file, put your folder
in the third line change the name of the file
D:
cd D:\python_proyects\example_folder\
python example_file.py
暂时将 python 添加到系统 PATH 可能会有所帮助:
Adding python to system PATH temporarily may help:
start xxx.py
您可以将其用于其他一些文件类型。
start xxx.py
You can use this for some other file types.
打开命令行(⊞ Win+R、
cmd
、↵ Enter)然后输入
python -V
,↵ Enter。您应该会收到响应,例如
Python 2.7.1
。如果不这样做,则可能没有安装 Python。首先解决这个问题。
一旦你有了Python,你的批处理文件应该看起来像这样。
这将使命令窗口在脚本完成后保持打开状态,以便你可以看到任何错误或消息。一旦您对此感到满意,您可以删除“暂停”行,命令窗口将在完成后自动关闭。
Open a command line (⊞ Win+R,
cmd
, ↵ Enter)and type
python -V
, ↵ Enter.You should get a response back, something like
Python 2.7.1
.If you do not, you may not have Python installed. Fix this first.
Once you have Python, your batch file should look like
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.
以下是如何将批处理代码和 python 代码放入单个文件中:
'''
分别开始和结束 python 多行注释。0<0# : ^
更有趣 - 由于批处理中的重定向优先级,它会被批处理脚本解释为:0<0# ^
,批处理脚本是一个标签,执行不会显示在屏幕上。末尾的插入符号将转义新行,第二行将附加到第一行。对于 python,它将是0<0
语句和内联注释的开头。功劳归于 siberia-man
Here's how you can put both batch code and the python one in single file:
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 be0<0
statement and a start of inline comment.The credit goes to siberia-man
只需在 python 脚本的同一文件夹中打开一个包含这两行的批处理文件:
Just simply open a batch file that contains this two lines in the same folder of your python script:
您可以直接在批处理文件中使用python代码,
https://gist.github.com/jadient/9849314。
请参阅解释,Python 命令行 -x 选项。
You can use python code directly in batch file,
https://gist.github.com/jadient/9849314.
See explanation, Python command line -x option.
如果您已将 Python 添加到您的 PATH 中,那么您也可以像这样简单地运行它。
If you've added Python to your PATH then you can also simply run it like this.
--- xxx.bat ---
--- yyy.py ---
--- xxx.bat ---
--- yyy.py ---
与 npocmaka 的解决方案类似,如果除了 python 代码之外,批处理文件中还有不止一行批处理代码,请查看以下内容:http://lallouslab.net/2017/06/12/batchography-embedding-python-scripts-in-your- batch-file-script/
这段代码的作用是通过将所有批处理代码放入多行字符串中,将自身作为 python 文件运行。该字符串的开头位于名为 rem 的变量中,以使批处理代码将其作为注释读取。由于
-x
参数,包含@echo off
的第一行在 Python 代码中被忽略。值得一提的是,如果您想在批处理代码中使用
\
,例如在文件路径中,则必须使用r"""..."""
将其包围起来,将其用作没有转义序列的原始字符串。Similar to npocmaka's solution, if you are having more than one line of batch code in your batch file besides the python code, check this out: http://lallouslab.net/2017/06/12/batchography-embedding-python-scripts-in-your-batch-file-script/
What this code does is it runs itself as a python file by putting all the batch code into a multiline string. The beginning of this string is in a variable called rem, to make the batch code read it as a comment. The first line containing
@echo off
is ignored in the python code because of the-x
parameter.it is important to mention that if you want to use
\
in your batch code, for example in a file path, you'll have to user"""..."""
to surround it to use it as a raw string without escape sequences.