Windows Cmd.exe窗口 [英] Windows Cmd.exe Window

查看:80
本文介绍了Windows Cmd.exe窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的罪过我是MS Windows用户在工作,除此之外我有一个小b $ b小问题...


我喜欢编写python脚本来执行小任务,然后从文件浏览器双击

来运行它们。


不幸的是我不完美,有时候我在运行脚本时犯了错误并且有未处理的异常或语法错误。

的默认行为是关闭命令窗口,这会让你无法读取异常。


过去我创建了.bat包装器文件,只调用python

解释器,但是为每个脚本创建一个匹配的.bat

文件有点乏味。所以我想出了以下方法...


1.将python.exe复制到pythoncmd.exe

2.在sitecustomize中添加一些东西.py

3.为每个python脚本添加一个特殊的第一行,并给它一个.cmd

扩展名。


添加到sitecustomize.py的东西(实际上我为此创建了一个

sitecustomize.py)是:

"""

import sys

import os


如果os.path.basename(sys.executable)==''pythoncmd.exe'':


def cmdexcepthook(* args):

sys .__ excepthook __(* args)

#让我们使用确认/检查错误

os.system(''pause'')


sys.excepthook = cmdexcepthook

"""


特殊的第一行是:


@pythoncmd -x"%~f0" %*&退出/ b


(在python.org的常见问题解答中,它表示

@setlocal enableextensions& python -x%~f0%*& goto :EOF

但是因为我不知道哪个是正确的我选择了更简单的

一个)


这个方法确实需要在你的%PATH%中使用pythoncmd.exe,但我认为这是合理的;)


我有点失望我无法''我想到了一种决定我是否运行.cmd的方法。在sitecustomize.py中的文件,以便我可以使用

普通的python.exe。使用重命名的解释器.exe只是一个技巧

用于检测我何时运行.cmd文件,但这意味着

脚本不会在另一台机器上运行没有将python.exe

复制到pythoncmd.exe上。这是一种耻辱。


所以我的问题。有没有更好的办法?我对这个

方法并不满意。我应该停止担心并去玩我的新夏威夷四弦琴吗?

请回答。


Giles

For my sins I''m a MS Windows user at work and apart from that I have a
small problem ...

I like to write python scripts to do small tasks and then double click
on them from the file explorer to run them.

Unfortunately I''m not perfect and sometimes I make mistakes and have
unhandled exceptions or syntax errors when running the scripts. The
default behaviour is to shut down the command window which leaves you
no chance of reading the exception.

In the past I have created .bat wrapper files that just call the python
interpreter, but it is a bit tedious to have to create a matching .bat
file for every script. So I came up with the following approach...

1. Copy python.exe to pythoncmd.exe
2. Add a bit of stuff to sitecustomize.py
3. Add a special first line to every python script and give it a .cmd
extension.

The stuff added to sitecustomize.py (actually I created a
sitecustomize.py for this) is:
"""
import sys
import os

if os.path.basename(sys.executable) == ''pythoncmd.exe'':

def cmdexcepthook(*args):
sys.__excepthook__(*args)
# Let use confirm/inspect error
os.system(''pause'')

sys.excepthook = cmdexcepthook
"""

The special first line is:

@pythoncmd -x "%~f0" %* & exit /b

(In the python.org FAQ for windows it says
@setlocal enableextensions & python -x %~f0 %* & goto :EOF
but since I have no idea which is "right" I chose the simpler looking
one)

This approach does require pythoncmd.exe to by in your %PATH% but I
think that is reasonable ;)

I am a bit disappointed I couldn''t think of a way of deciding if I was
running a ".cmd" file in sitecustomize.py so that I could just use the
normal python.exe. Using a renamed interpreter .exe is just a trick
for detecting when I am running .cmd files, but it means that the
script won''t run on another machine that hasn''t had the python.exe
copied to pythoncmd.exe on it. Which is a shame.

So my question. Is there a better way? I''m not really happy with this
approach. Should I stop worrying and go and play my new ukulele?
Answers please.

Giles

推荐答案

* Giles Brown(2005-07-07 13:56 +0100)
* Giles Brown (2005-07-07 13:56 +0100)
对于我的罪过我是MS Windows用户,在工作中除此之外我有一个小问题...

我喜欢编写python脚本来完成小任务,然后从文件资源管理器中双击
来运行它们。不幸的是,我不完美,有时候我会犯错误,并且在运行脚本时会出现未处理的异常或语法错误。
默认行为是关闭命令窗口,这使您无法读取异常。

过去我创建了.bat包装文件,只需调用python
解释器,但是为每个脚本创建一个匹配的.bat
文件有点乏味。所以我想出了以下方法......

1.将python.exe复制到pythoncmd.exe
2.在sitecustomize.py中添加一些东西
3。为每个python脚本添加一个特殊的第一行并给它一个.cmd
扩展名。

添加到sitecustomize.py的东西(实际上我为此创建了一个
sitecustomize.py )是:
"""
import sys
导入os
如果os.path.basename(sys.executable)==''pythoncmd。 exe'':

def cmdexcepthook(* args):
sys .__ excepthook __(* args)
#let use confirm / inspect error
os.system(' '暂停'')

sys.excepthook = cmdexcepthook
"""

特殊的第一行是:

@pythoncmd -x"%~f0" %*&退出/ b

(在python.org的常见问题解答中,它表示
@setlocal enableextensions& python -x%~f0%*& goto:EOF
但是我不知道哪个是正确的我选择了更简单的一个

这种方法确实需要在你的%PATH%中使用pythoncmd.exe但是我认为这是合理的;)

我有点失望,我无法想出一种决定我是否运行.cmd的方法。在sitecustomize.py中的文件,以便我可以使用
正常的python.exe。使用重命名的解释器.exe只是一个技巧,用于检测我何时运行.cmd文件,但这意味着
脚本不会在另一台没有python的机器上运行.exe
复制到pythoncmd.exe就可以了。这是一种耻辱。

所以我的问题。有没有更好的办法?我对这种方法并不满意。我应该停止担心并去玩我的新夏威夷四弦琴吗?
请回答。
For my sins I''m a MS Windows user at work and apart from that I have a
small problem ...

I like to write python scripts to do small tasks and then double click
on them from the file explorer to run them.

Unfortunately I''m not perfect and sometimes I make mistakes and have
unhandled exceptions or syntax errors when running the scripts. The
default behaviour is to shut down the command window which leaves you
no chance of reading the exception.

In the past I have created .bat wrapper files that just call the python
interpreter, but it is a bit tedious to have to create a matching .bat
file for every script. So I came up with the following approach...

1. Copy python.exe to pythoncmd.exe
2. Add a bit of stuff to sitecustomize.py
3. Add a special first line to every python script and give it a .cmd
extension.

The stuff added to sitecustomize.py (actually I created a
sitecustomize.py for this) is:
"""
import sys
import os

if os.path.basename(sys.executable) == ''pythoncmd.exe'':

def cmdexcepthook(*args):
sys.__excepthook__(*args)
# Let use confirm/inspect error
os.system(''pause'')

sys.excepthook = cmdexcepthook
"""

The special first line is:

@pythoncmd -x "%~f0" %* & exit /b

(In the python.org FAQ for windows it says
@setlocal enableextensions & python -x %~f0 %* & goto :EOF
but since I have no idea which is "right" I chose the simpler looking
one)

This approach does require pythoncmd.exe to by in your %PATH% but I
think that is reasonable ;)

I am a bit disappointed I couldn''t think of a way of deciding if I was
running a ".cmd" file in sitecustomize.py so that I could just use the
normal python.exe. Using a renamed interpreter .exe is just a trick
for detecting when I am running .cmd files, but it means that the
script won''t run on another machine that hasn''t had the python.exe
copied to pythoncmd.exe on it. Which is a shame.

So my question. Is there a better way? I''m not really happy with this
approach. Should I stop worrying and go and play my new ukulele?
Answers please.




我认为这是一个常见问题:最后使用raw_input阻止关闭。



I think it''s a FAQ to: use raw_input at the end to prevent closing.


Nah。你错过了我的观点。如果有*异常*,我只希望命令窗口不要关闭
。挑剔我知道,但你去了。


Giles

Nah. You''re missing my point. I only want the command window not to
be closed if there is an *exception*. Picky I know, but there you go.

Giles




开05.07.2005,时间15:25,Giles Brown写道:

On 07.07.2005, at 15:25, Giles Brown wrote:
Nah。你错过了我的观点。如果有*异常*,我只希望关闭命令窗口。挑剔我知道,但你去了。
Nah. You''re missing my point. I only want the command window not to
be closed if there is an *exception*. Picky I know, but there you go.




好​​,然后将raw_input注册为退出函数:



well, then register raw_input as exit function:

import atexit
atexit.register(raw_input)
import atexit
atexit.register(raw_input)




工作正常在我的终端。也应该在你的框架中做。


欢呼,


- 哈罗德 -


-

什么是心灵? - 没关系。

什么事? - 没关系!

-



works fine in my terminal. should do in your framework also.

cheers,

- harold -

--
What is mind? -- Doesn''t matter.
What is matter? -- Never mind!
--


这篇关于Windows Cmd.exe窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆