【发布时间】:2021-05-05 11:23:05
【问题描述】:
.bat 文件:
@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
.py 文件:
#! python3
print('Hello World, this is a test program for showing the use of .bat batch files, and the role of the shebang line.')
当我在 PowerShell 或命令提示符中运行 .bat 文件时:
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
附注:
- 各自文件(.py 和 .bat)的各自路径没有任何错误。
- 我也试过
@py.exe而不是@py - 在环境变量中,PATH 变量也相应设置
- 我还尝试从 .py 文件中删除 %*
- 参考:Book: Automate the Boring Stuff with Python (Appendix B)
我该如何解决这个问题?
【问题讨论】:
-
每一行都做完全不同的事情。首先您有一个调用
py的.bat文件,然后您尝试使用py调用批处理文件?你试过只执行批处理文件吗? -
是的,我尝试只执行批处理文件 (.\BatchFile-TestProgram.bat)。但它仍然没有工作。 PowerShell 中的输出:'C:\Users\Universal' 不是内部或外部命令、可运行程序或批处理文件。
-
那是因为您忘记将文件括在双引号中。当文件包含空格时,无论操作系统如何,您必须转义空格或以某种方式告诉操作系统这是一个长文件名而不是多个参数
标签: python windows batch-file command-prompt python-3.4