.bat 文件不起作用 |运行 .bat 文件时显示无效语法 | Python 3.4.0

Posted

技术标签:

【中文标题】.bat 文件不起作用 |运行 .bat 文件时显示无效语法 | Python 3.4.0【英文标题】:.bat file doesn't work | Shows invalid syntax when .bat file is run | Python 3.4.0 【发布时间】: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' 不是内部或外部命令、可运行程序或批处理文件。 那是因为您忘记将文件括在双引号中。当文件包含空格时,无论操作系统如何,您必须转义空格或以某种方式告诉操作系统这是一个长文件名而不是多个参数 【参考方案1】:

问题是

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

将尝试使用 Python 解释器运行 .bat 文件。这是一个错误,因为 Python 解释器理解 Python 语言,但不理解编写 .bat 文件的 .bat/Powershell 语言。

@py C:\Users\... 已经是无效的 Python 语法,因为 @py 被视为装饰器,并且装饰器后面不能跟 C 这样的符号名称。

如何解决这个问题:使用 Powershell 运行 .bat 文件(假设 .bat 文件本身是正确的)或完全丢弃 .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"

如果您希望 Python 代码暂停(例如 @pause),您可以在脚本末尾请求用户输入:

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

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

【讨论】:

@PanagiotisKanavos,第二段代码说 OP 正在尝试运行py "C:\...\BatchFile-TestProgram.bat",它将尝试使用 Python 解释器运行 BAT 文件。 我被太长的路径弄糊涂了,没有注意到 CLI 试图做批处理本身试图做的事情。 在 PowerShell 中运行:.\BatchFile-TestProgram.bat(位于正确位置)输出:'C:\Users\Universal' 未被识别为内部或外部命令、可运行程序或批处理文件。跨度> 【参考方案2】:

不要使用py标签,只需简单地写下.bat文件的路径即可:

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

如果您使用.bat 文件运行此代码:

print("Hello")

输出将是:

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 . . .

【讨论】:

py 文件只是文本,它们不能自己运行。您确实需要py pathTo.pypython pathTo.py。问题是 OP 试图将批处理文件路径到py 而不是仅仅执行批处理文件 在哪里工作?在Start 对话框中?还是命令行界面?这是一个基本概念,而不是works on my PC 的东西。 Something 必须将 any 文档扩展名与适当的应用程序相关联。当您使用Start 时,shell 将根据扩展找到应用程序并将路径提供给它 当我从命令提示符运行.bat 文件时,我可以看到答案中包含的输出。 我运行了没有py标签的批处理文件,但我仍然得到同样的“无效语法”错误。 如何在命令提示符下运行.bat文件?【参考方案3】:

各位,我终于解决了!非常感谢大家回答我的问题或通过 cmets 提供反馈!我非常感谢您宝贵的时间来帮助像我这样的菜鸟。谢谢!:)

好的,那么解决方法:

首先,我对我的 .bat 文件/批处理文件做了一点改动。 我将 .py 文件的路径用双引号 (") 括起来

@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

最后,我没有在其位置路径的开头运行 .bat 文件,而是运行 .bat 文件。 在我的 PowerShell 中,我移动了到 .bat 文件的目录,然后运行 ​​.bat 文件:

.\BatchFile-TestProgram.bat

它返回了正确的输出:

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 . . .

另外,我能够从运行对话框 (WIN + R) 运行批处理文件。输出与直接在 PowerShell 中运行批处理文件相同。我刚刚输入了批处理文件的完整路径,用双引号括起来:

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

我学到了什么:

py 执行用 Python 编写的文件。它不执行 .bat 文件,因为 Python 解释器不理解写入 .bat 文件的 CMD 语法。 确保使用引号(双引号或单引号)来封闭路径非常重要,尤其是在写入文件夹或文件的路径时单词之间有空格时。 当然,如果您在命令提示符中,则不能使用单引号将路径括起来,因为 CMD 不会将单引号视为常规字符。 “@”告诉命令提示符在运行程序(.py 文件)时不要显示整行(路径或命令“暂停”),而只是执行程序。

【讨论】:

@ForceBru 感谢您的宝贵时间和反馈! @Jakub Szlaur 感谢您的宝贵时间和反馈! @PanagiotisKanavos 也感谢您的时间和反馈!

以上是关于.bat 文件不起作用 |运行 .bat 文件时显示无效语法 | Python 3.4.0的主要内容,如果未能解决你的问题,请参考以下文章

bat处理文件

powershell $ lastexitcode在运行批处理文件时不起作用

从bat文件设置类路径不起作用[重复]

运行 .bat 文件的命令

在 .bat 文件中使用 Python 导入 pandas 时不起作用

“npm config set registry https://registry.npmjs.org/”在 Windows bat 文件中不起作用