.bat file doesn't work | Shows invalid syntax when .bat file is run | Python 3.4.0

1.1k views Asked by At

The .bat file:

@py C:\Users\Universal Sysytem\Desktop\Python Scripts (Automate the Boring Stuff)\Automate the Boring Stuff with Python\TestProgram(.bat batch file and shebang line usecase).py %*

@pause

The .py file:

#! python3

print('Hello World, this is a test program for showing the use of .bat batch files, and the role of the shebang line.')

When I run the .bat file in PowerShell or Command Prompt:

PS C:\Users\Universal Sysytem>  py "C:\Users\Universal Sysytem\Desktop\Python Scripts (Automate the Boring Stuff)\Automate the Boring Stuff with Python\BatchFile-TestProgram.bat"
  File "C:\Users\Universal Sysytem\Desktop\Python Scripts (Automate the Boring Stuff)\Automate the Boring Stuff with Python\BatchFile-TestProgram.bat", line 1
    @py C:\Users\Universal Sysytem\Desktop\Python Scripts (Automate the Boring Stuff)\Automate the Boring Stuff with Python\TestProgram(.bat batch file and shebang line usecase).py %*
        ^
SyntaxError: invalid syntax

P.S:

  • The respective paths to the respective files (.py and .bat) do not have any errors.
  • I also tried @py.exe instead of @py
  • In Environment Variables, the PATH variable is also set accordingly
  • I also tried removing %* from inside the .py file
  • Reference: Book: Automate the Boring Stuff with Python (Appendix B)

How do I resolve this issue?

3

There are 3 answers

3
Fateen On BEST ANSWER

Guys, I finally solved it! Thank you so much, everybody, who answered my question or gave feedback through comments! I am very grateful for your valuable time in trying to help a noob like me. Thank you!:)

Okay, so the solution:

First of all, I made a slight change to my .bat file/batch file. I enclosed the path to the .py file in double-quotes("):

@py "C:\Users\Universal Sysytem\Desktop\Python Scripts (Automate the Boring Stuff)\Automate the Boring Stuff with Python\TestProgram(.bat batch file and shebang line usecase).py"
@pause

Finally, instead of running the .bat file with py at the beginning of its location's path, I just ran the .bat file. In my PowerShell, I moved to the directory of the .bat file and then just ran the .bat file:

.\BatchFile-TestProgram.bat

It returned the correct output:

Hello World, this is a test program for showing the use of .bat batch files, and the role of the shebang line.
Press any key to continue . . .

Also, I was able to run the batch file from the Run Dialog (WIN + R). The output was the same as running the batch file directly in PowerShell. I just entered the complete path to the batch file, enclosing it in double-quotes:

"c:\users\Universal Sysytem\Desktop\Python Scripts (Automate the Boring Stuff)\Automate the Boring Stuff with Python\BatchFile-TestProgram.bat"

What I learned:

  • py executes files written in Python. It doesn't execute .bat files because Python interpreter does not understand CMD syntax in which the .bat file is written.
  • Important to make sure to use quotes (double or single) for enclosing paths, especially when there are blank spaces between words when writing paths to folders or files. Of course, you can't use single quotes for enclosing paths if you're in Command Prompt because CMD doesn't treat single quotes as anything but a regular character.
  • The '@' tells the command prompt not to display the entire line (path or the command 'pause') when running the program (.py file) but just execute the program.
5
Jakub Szlaur On

Don't use the py tags just simple write down files path to the .bat file:

C:\My\Path\To\stack.py
pause

If you use the .bat file to run this code:

print("Hello")

The output will be:

C:\My\Path\To\stack.py>C:\My\Path\To\stack.py\stack.py
Hello

C:\My\Path\To\stack.py>pause
Press any key to continue . . .
3
ForceBru On

The problem is that

py "C:\...\BatchFile-TestProgram.bat"

will try to run the .bat file with the Python interpreter. This is an error because the Python interpreter understands Python language but doesn't understand .bat/Powershell language that the .bat file is written in.

@py C:\Users\... is already invalid Python syntax because @py is treated as a decorator, and decorators can't be followed by a symbol name like C.

How to solve this: run the .bat file with Powershell (assuming the .bat file itself is correct) or throw away the .bat file entirely and simply run:

py "C:\Users\Universal Sysytem\Desktop\Python Scripts (Automate the Boring Stuff)\Automate the Boring Stuff with Python\TestProgram(.bat batch file and shebang line usecase).py"

If you want your Python code to pause (like with @pause), you can request user input at the end of your script:

print("This is my script, hello!")
# run some code...

# wait for input, then exit
input("Press ENTER to exit...")