在批处理脚本中运行 Python 文件

MD Aminul Islam 2023年1月30日 2022年7月12日
  1. 直接引用 Python 脚本(方法一)
  2. 参照批处理脚本(方法二)
在批处理脚本中运行 Python 文件

在本文中,我们将讨论一些使用批处理脚本运行 Python 文件的方法。在我们继续之前,你需要检查你的系统是否在你的系统环境中包含 Python。

你可以在 Windows CMD 中使用以下命令进行检查。

命令:

python -V

如果此命令的输出类似于 Python 2.7.1,那么没关系。否则,你需要下载 Python 并将其设置为你的系统环境。

直接引用 Python 脚本(方法一)

在你的系统上安装 Python 后,你需要找到名为 python.exe 的文件。

语法:

D:\YourLocationTo\python.exe D:\Directory\Yourfile.py

然后使用以下命令。

命令:

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

在上面的示例中,我们运行名为 TextPython.py 的文件。

参照批处理脚本(方法二)

这样,我们将创建一个运行 Python 脚本的批处理脚本。

语法:

python c:\Directory\YourPythonScript.py %*

让我们举一个使用这个命令的例子。在下面的示例中,我们将通过 Batch 脚本运行名为 Yourfile.py 的 Python 脚本。

命令:

@echo off
PYTHON D:\Directory\Yourfile.py %*
PAUSE

使用上述共享方法,你可以通过 Batch 脚本运行 Python 文件。请记住,你需要检查你的系统中是否启用了 Python 作为系统环境。

如果不启用 Python,这些方法将不起作用。请注意,我们在本文中分享的代码是用 Batch Script 编写的,并且只能在 Windows 命令提示符环境中运行。

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

相关文章 - Batch Script