Execute python scripts in bat

environment

  • windows 10 64bit
  • anaconda with python 3.8

Introduction

Many python projects are directly executed by scripts, which is actually very unfriendly to some little white children’s shoes who have not been exposed to programming, and it is easy to make mistakes when typing the code by themselves. Integrating python scripts into batch files is actually a good solution. It can be operated with a click of the mouse, and it is still very user-friendly. This article combines an example to see the implementation steps.

Practical

Prepare a simple python script here

 '''打印出当前文件夹下的所有文件的文件名''' import os if __name__ == '__main__': path = os.listdir('.') for p in path: print('filename: {}'.format(p))

Then in the same directory, create a new batch file run. bat , and fill in the content in the file

 @echo off cmd /k D:\Tools\anaconda3\envs\streamlit\python.exe D:\test\test.py

in

  • echo off: turn off command echo, turn on echo is echo on. The command mentioned here is the cmd command below in this example
  • cmd: Create a new cmd window
  • /k: Keep the window after executing the command, /c is to close the window after executing
  • D:\Tools\anaconda3\envs\streamlit\python.exe D:\test\test.py: Execute the command of the python script, I am using the anaconda environment here

Once done, double-click run.bat

This article is transferred from https://xugaoxiang.com/2023/03/20/bat-call-python-script/
This site is only for collection, and the copyright belongs to the original author.