在批處理指令碼中執行 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