如何解决.bat 文件不起作用 |运行 .bat 文件时显示无效语法 |蟒蛇 3.4.0
.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 "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...")
,
不要使用 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 . . .
,
伙计们,我终于解决了!非常感谢大家回答我的问题或通过评论提供反馈!我非常感谢您抽出宝贵时间帮助像我这样的菜鸟。谢谢!:)
好的,那么解决方案:
首先,我对我的 .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
最后,我没有在其位置路径的开头运行带有 py 的 .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 文件)时不显示整行(路径或命令 'pause'),而只是执行程序。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。