首页 > 解决方案 > 在我的 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 即可。

然后我再次按照这些步骤操作,但效果不佳。你能帮我解决这个问题吗?谢谢你。

标签: pythonpython-3.xbatch-file

解决方案


以下是我建议的故障排除步骤:

  1. 确保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 提供了全面的说明。

  2. 您提出问题的方式似乎表明您将整个代码保存在一个文件中pw.bat。如果你这样做了,你需要确保以下几点:

    一个。您的代码片段的最后两行在一个文件pw.bat中,并且,

    湾。其余代码在文件中C:\py\Automate the Boring Stuff with Python\Data Structures, Dictionary\pw.py

  3. 仅当文件pw.bat位于环境路径中并因此对操作系统可见时,以下说明才是正确的:

    创建此批处理文件后,在 Windows 上运行密码安全程序只需按 win-R 并键入 pw

    如果您不知道这是否属实,则需要找出您保存的位置pw.bat。打开命令提示符并导航到该位置,然后执行pw. 此处讨论与此相关的问题。


推荐阅读