Skip to content

Instantly share code, notes, and snippets.

@jadient
Last active October 21, 2024 22:11
Show Gist options
  • Save jadient/9849314 to your computer and use it in GitHub Desktop.
Save jadient/9849314 to your computer and use it in GitHub Desktop.
Run python code directly from a batch file
@echo off & python -x "%~f0" %* & goto :eof
# ==========================================================
# one way to place python script in a batch file
# place python code below (no need for .py file)
# ==========================================================
import sys
print "Hello World!"
for i,a in enumerate(sys.argv):
print "argv[%d]=%s" % (i,a)
@p4535992
Copy link

very useful ty

@selenagomez13
Copy link

How do I run a batch code after that python script (line 11)?

@x68507
Copy link

x68507 commented Jun 28, 2016

what if I want to run some BAT code before calling the Python line? Even if I insert a blank line at #1 and have the @echo off & python on #2 I get a SyntaxError: invalid syntax.

@hginzel
Copy link

hginzel commented Jan 4, 2019

@jenniferlamptey31
Copy link

I would like to know how to run a batch script after that python script at line 11

@jadient
Copy link
Author

jadient commented Mar 5, 2022

To run commands before or after, create a new batch file that calls this batch file, for example:

echo About to call the python batchfile
call batchfile.bat
echo Return from the python batchfile

python reads the entire contents of batchfile.bat and skips the first line. there's no way to change that.

@jenniferlamptey31
Copy link

jenniferlamptey31 commented Mar 5, 2022

The problem is I don't want to include or embed another .bat file. I want all these in one single file because I will be publishing the batch file as an .exe file to stop others from stealing code and I don't want them seeing the included files.

@yauheni-dz
Copy link

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment