python - 在我的 python 脚本中创建批处理文件失败的原因是什么?
问题描述
我正在学习automate the boring stuff with python
这本书,在完成password locker
项目后建议使用以下代码创建一个批处理文件:
@py.exe C:\Python34\pw.py %*
@pause
我做了完全一样的我的一个看起来像这样(如果有任何其他潜在的错误,复制整个脚本):
import sys
import pyperclip
""" PASSWORD LOCKER """
passwords = {
'facebook' : 'password of facebook',
'gmail' : 'password of gmail',
'quora' : 'password of quora'
}
if len(sys.argv) == 2:
account_name = sys.argv[1]
if account_name in passwords:
print('Password of [' + str(account_name).upper() + '] has been copied to clipboard.')
acc_password = passwords[account_name]
pyperclip.copy(acc_password)
print('Paste it anywhere')
else:
print('There is no name registered with that account name.')
@py.exe 'C:\py\Automate the Boring Stuff with Python\Data Structures, Dictionary\pw.py' %*
@pause
然后根据书中的说明将文件另存为pw.bat
:
创建此批处理文件后,在 Windows 上运行密码安全程序只需按下 win-R 并键入 pw 即可。
然后我再次按照这些步骤操作,但效果不佳。你能帮我解决这个问题吗?谢谢你。
解决方案
以下是我建议的故障排除步骤:
确保
py.exe
存在于您计算机的环境路径中。书中可能包含无意中跳过的安装说明。如果您知道py.exe
计算机上存在,但是当您py.exe
在打开命令提示符时执行时,您可能会看到:'py.exe' is not recognized as an internal or external command, operable program or batch file.
如果您看到上述消息,这意味着您需要执行以下任一操作:
一个。将批处理文件更改为:
`@<drive:\path\to>\py.exe`
其中部分
<drive:\path\to>
是您的 python 可执行文件 (py.exe
) 的路径,或者,湾。将路径添加
py.exe
到环境变量。此处为 Python 3 提供了全面的说明。您提出问题的方式似乎表明您将整个代码保存在一个文件中
pw.bat
。如果你这样做了,你需要确保以下几点:一个。您的代码片段的最后两行在一个文件
pw.bat
中,并且,湾。其余代码在文件中
C:\py\Automate the Boring Stuff with Python\Data Structures, Dictionary\pw.py
。仅当文件
pw.bat
位于环境路径中并因此对操作系统可见时,以下说明才是正确的:创建此批处理文件后,在 Windows 上运行密码安全程序只需按 win-R 并键入 pw
如果您不知道这是否属实,则需要找出您保存的位置
pw.bat
。打开命令提示符并导航到该位置,然后执行pw
. 此处讨论与此相关的问题。
推荐阅读
- html - 如何在不打开新标签的情况下使用 javascript 下载 html5 视频?
- docker - Microsoft SDK docker 映像可以构建但不能运行 Web 应用程序
- ios - 如何在 UIGraphicsImageRenderer 中绘制对象之前对其进行旋转?
- python-3.x - 硒,键盘无法访问的元素
- c# - 使用 JWT 令牌身份验证访问在 VB.Net 2 webforms 和 MySQL 中开发的另一个域/Web 应用程序的可能方法
- json - Django / Fetch:从请求的数据流中读取后无法访问正文
- google-apps-script - 谷歌表格:“getrange 不是函数”
- html - 最初应用 CSS 转换并在点击时通过转换移除?
- twilio - 如何通知移动应用程序进行 Twilio 呼入电话?已经使用节点服务器测试 Twilio 演示 android 应用程序,但它不工作
- python - 模块已安装但在导入文件中找不到