多重剪贴板【Python】

来源:互联网 发布:ps4 pro优化游戏 编辑:程序博客网 时间:2024/06/08 09:02

跟着《Python编程快速上手  让繁琐工作自动化》这本书学Python,按照书上的步骤来,第二次遇到自己解决不了的问题:

E:\Python\python.exe: can't open file 'mcb.pyw': [Errno 2] No such file or directory

题是:

假定你有一个无聊的任务,要填充一个网页或软件中的许多表格,其中包含一
些文本字段。剪贴板让你不必一次又一次输入同样的文本,但剪贴板上一次只有一
个内容。如果你有几段不同的文本需要拷贝粘贴,就不得不一次又一次的标记和拷
贝几个同样的内容。
可以编写一个 Python 程序,追踪几段文本。这个“多重剪贴板”将被命名为
mcb.pyw(因为“mcb”比输入“multiclipboard”更简单)。.pyw 扩展名意味着 Python
运行该程序时,不会显示终端窗口(详细内容请参考附录 B)。
该程序将利用一个关键字保存每段剪贴板文本。例如,当运行 py mcb.pyw save
spam,剪贴板中当前的内容就用关键字 spam 保存。通过运行 py mcb.pyw spam,这
段文本稍后将重新加载到剪贴板中。如果用户忘记了都有哪些关键字,他们可以运
行 py mcb.pyw list,将所有关键字的列表复制到剪贴板中。
下面是程序要做的事:
• 针对要检查的关键字,提供命令行参数。
• 如果参数是 save,那么将剪贴板的内容保存到关键字。
• 如果参数是 list,就将所有的关键字拷贝到剪贴板。
• 否则,就将关键词对应的文本拷贝到剪贴板。
这意味着代码需要做下列事情:
• 从 sys.argv 读取命令行参数。
• 读写剪贴板。
• 保存并加载 shelf 文件。

按照书上给的解法是:

1.编写python程序mcb.pyw 在我电脑上的路径为:E:\Python\practice\part_two\Chart_Eight_Write_Read\mcb.pyw,这个是拓展后的,增加了delete功能

#! python3# mcb.pyw - Saves and loads pieces of text to the clipboard.# Usage: py.exe mcb.pyw save <keyword> - Saves clipboard to keyword.# py.exe mcb.pyw <keyword> - Loads keyword to clipboard.# py.exe mcb.pyw list - Loads all keywords to clipboard.import shelve, pyperclip, sysmcbShelf = shelve.open('mcb')# Save clipboard content.if len(sys.argv) == 3:    if sys.argv[1].lower() == 'save':        mcbShelf[sys.argv[2]] = pyperclip.paste()    elif sys.argv[1].lower() == 'delete':        del mcbShelf[sys.argv[2]]elif len(sys.argv) == 2:    # List keywords and load content.    if sys.argv[1].lower() == 'list':        pyperclip.copy(str(list(mcbShelf.keys())))    elif sys.argv[1] in mcbShelf:        pyperclip.copy(mcbShelf[sys.argv[1]])    elif sys.argv[1].lower() == 'delete':        mcbShelf.clear()mcbShelf.close()

2.编写脚本文件:mcb.bat,存放在C盘windows文件夹下

@pyw.exe E:\Python\practice\part_two\Chart_Eight_Write_Read\mcb.pyw %*@pause

3.运行程序 :windows^R  -> cmd-> enter

输入 py mcb.pyw save spam,就出现了上述错误:

E:\Python\python.exe: can't open file 'mcb.pyw': [Errno 2] No such file or directory

程序,脚本文件均没问题,随百度,没找到中文回答,只好硬着头皮看英文论坛,然后发现读英文句子并没有像想象中的那么恐怖。下面是我百度到的:

The batch file will then run the command pyw.exe C:\Users\johng\mypythonscripts\mcb.pyw [Optional arguments here]which is what actually calls the python script. In effect, you never call the python script directly, you call the batch file which calls the python script for you.

The reason why using cd to get to the directory first allows your command to work, is that you're actually skipping the batch file entirely and doing all of the work the batch file is supposed to do for you. The batch file is designed to take all of the typing you're doing now and shorten it down to just typing mcb

If you want to use it the way it's originally designed, I'd suggest trying to just type mcb save spam in your command line. This should call the batch file, which then calls the python script for you, and you should be able to run it no matter what your current working directory is.

翻译:大致意思就是py mcb.pym save spam 并没有通过脚本文件执行程序,而是在配置好Python的前提下,在mcb.pym的路径下,可以直接这样运行。

但是,如果想通过脚本文件执行程序,就应该直接是脚本文件名 +参数,所以,这是两种不同的执行方式。那么第一种,题里已经给出来了,下面是第二种:

mcb save spam:保存剪贴板里的内容,关键字为spam

mcb list : 把所有的关键字列表复制到剪贴板中

mcb spam : 把关键字为spam所对应的内容复制到剪贴板中。

拓展:

mcb delete spam : 删除关键字为spam的键值对信息

mcb delete : 清空列表。