【问题】

这里提到的,打包python中,由于python中调用windows的cmd去执行一些动作,所以打包后的python,结果还是会遇到,调用cmd窗口(执行了对应的命令后)一闪而过。

想要消除此cmd一闪而过的问题。

只希望运行命令,执行动作。彻底不希望看到cmd的窗口显示出来。

【解决过程】

1.网上搜了搜,是关于python调用cmd的一些内容,所以说了:

设置shell为false,类似于:

subprocess.call('cmd.exe', shell=False, ......)

3.或者:

给cmd.exe 添加/Q参数,类似于:

subprocess.call('cmd.exe /Q', shell=False, ......)

但是结果不行:

有朝这方向想过,上面的我试过不行

shell默认为False,为真的话,unix下相当于args前面添加了 "/bin/sh" "-c",window下,相当于添加"cmd.exe /c",和隐藏shell窗口没关系吧

2.也去看了看,cmd本身的一些参数支持:

Microsoft Windows [Version 6.1.7601]

Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\CLi>cmd /?

Starts a new instance of the Windows command interpreter

CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]

[[/S] [/C | /K] string]

/C Carries out the command specified by string and then terminates

/K Carries out the command specified by string but remains

/S Modifies the treatment of string after /C or /K (see below)

/Q Turns echo off

/D Disable execution of AutoRun commands from registry (see below)

/A Causes the output of internal commands to a pipe or file to be ANSI

/U Causes the output of internal commands to a pipe or file to be

Unicode

/T:fg Sets the foreground/background colors (see COLOR /? for more info)

/E:ON Enable command extensions (see below)

/E:OFF Disable command extensions (see below)

/F:ON Enable file and directory name completion characters (see below)

/F:OFF Disable file and directory name completion characters (see below)

/V:ON Enable delayed environment variable expansion using ! as the

delimiter. For example, /V:ON would allow !var! to expand the

variable var at execution time. The var syntax expands variables

at input time, which is quite a different thing when inside of a FOR

loop.

/V:OFF Disable delayed environment expansion.

Note that multiple commands separated by the command separator ‘&&’

are accepted for string if surrounded by quotes. Also, for compatibility

reasons, /X is the same as /E:ON, /Y is the same as /E:OFF and /R is the

same as /C. Any other switches are ignored.

If /C or /K is specified, then the remainder of the command line after

the switch is processed as a command line, where the following logic is

used to process quote (") characters:

1. If all of the following conditions are met, then quote characters

on the command line are preserved:

– no /S switch

– exactly two quote characters

– no special characters between the two quote characters,

where special is one of: &<>()@^|

– there are one or more whitespace characters between the

two quote characters

– the string between the two quote characters is the name

of an executable file.

2. Otherwise, old behavior is to see if the first character is

a quote character and if so, strip the leading character and

remove the last quote character on the command line, preserving

any text after the last quote character.

If /D was NOT specified on the command line, then when CMD.EXE starts, it

looks for the following REG_SZ/REG_EXPAND_SZ registry variables, and if

either or both are present, they are executed first.

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun

and/or

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

Command Extensions are enabled by default. You may also disable

extensions for a particular invocation by using the /E:OFF switch. You

can enable or disable extensions for all invocations of CMD.EXE on a

machine and/or user logon session by setting either or both of the

following REG_DWORD values in the registry using REGEDIT.EXE:

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\EnableExtensions

and/or

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\EnableExtensions

to either 0x1 or 0x0. The user specific setting takes precedence over

the machine setting. The command line switches take precedence over the

registry settings.

In a batch file, the SETLOCAL ENABLEEXTENSIONS or DISABLEEXTENSIONS arguments

takes precedence over the /E:ON or /E:OFF switch. See SETLOCAL /? for details.

The command extensions involve changes and/or additions to the following

commands:

DEL or ERASE

COLOR

CD or CHDIR

MD or MKDIR

PROMPT

PUSHD

POPD

SET

SETLOCAL

ENDLOCAL

IF

FOR

CALL

SHIFT

GOTO

START (also includes changes to external command invocation)

ASSOC

FTYPE

To get specific details, type commandname /? to view the specifics.

Delayed environment variable expansion is NOT enabled by default. You

can enable or disable delayed environment variable expansion for a

particular invocation of CMD.EXE with the /V:ON or /V:OFF switch. You

can enable or disable delayed expansion for all invocations of CMD.EXE on a

machine and/or user logon session by setting either or both of the

following REG_DWORD values in the registry using REGEDIT.EXE:

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\DelayedExpansion

and/or

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\DelayedExpansion

to either 0x1 or 0x0. The user specific setting takes precedence over

the machine setting. The command line switches take precedence over the

registry settings.

In a batch file the SETLOCAL ENABLEDELAYEDEXPANSION or DISABLEDELAYEDEXPANSION

arguments takes precedence over the /V:ON or /V:OFF switch. See SETLOCAL /?

for details.

If delayed environment variable expansion is enabled, then the exclamation

character can be used to substitute the value of an environment variable

at execution time.

You can enable or disable file name completion for a particular

invocation of CMD.EXE with the /F:ON or /F:OFF switch. You can enable

or disable completion for all invocations of CMD.EXE on a machine and/or

user logon session by setting either or both of the following REG_DWORD

values in the registry using REGEDIT.EXE:

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\CompletionChar

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\PathCompletionChar

and/or

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\CompletionChar

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\PathCompletionChar

with the hex value of a control character to use for a particular

function (e.g. 0x4 is Ctrl-D and 0x6 is Ctrl-F). The user specific

settings take precedence over the machine settings. The command line

switches take precedence over the registry settings.

If completion is enabled with the /F:ON switch, the two control

characters used are Ctrl-D for directory name completion and Ctrl-F for

file name completion. To disable a particular completion character in

the registry, use the value for space (0x20) as it is not a valid

control character.

Completion is invoked when you type either of the two control

characters. The completion function takes the path string to the left

of the cursor appends a wild card character to it if none is already

present and builds up a list of paths that match. It then displays the

first matching path. If no paths match, it just beeps and leaves the

display alone. Thereafter, repeated pressing of the same control

character will cycle through the list of matching paths. Pressing the

Shift key with the control character will move through the list

backwards. If you edit the line in any way and press the control

character again, the saved list of matching paths is discarded and a new

one generated. The same occurs if you switch between file and directory

name completion. The only difference between the two control characters

is the file completion character matches both file and directory names,

while the directory completion character only matches directory names.

If file completion is used on any of the built in directory commands

(CD, MD or RD) then directory completion is assumed.

The completion code deals correctly with file names that contain spaces

or other special characters by placing quotes around the matching path.

Also, if you back up, then invoke completion from within a line, the

text to the right of the cursor at the point completion was invoked is

discarded.

The special characters that require quotes are:

&()[]{}^=;!’+,`~

3.再去搜:

消除cmd一闪而过

找到一些参考资料:

解决cmd命令控制台窗口一闪而过运行后就消失的方法

结果根本不是想要的.

4.参考这里,好像是可以通过最开始先用

?

1

@echo off

然后再加上要运行的命令,好像就可以不显示输出了。

但是貌似这和python中调用cmd,也还是不一样。

不过有机会可以去试试。。。

5.不过刚又注意到,其实cmd的:

/Q Turns echo off

就应该等价于上述的

@echo off

才对。

6.这里提到了三种方式:

WinExec(TEXT("net user abc /add"),SW_HIDE);

CreateProcess

隐藏cmd,并用管道实现命令运行

WinExec有时候好像不能运行,不知道为什么。

CreateProcess 第二个参数直接写入命令就行了,第一个留空(NULL),。后面属性里面加隐藏。

管道最麻烦,不过用起来交互是最好的。

但是不知道如何操作。

7.找到关于CreateProcess的示例了:

隐藏cmd窗口执行dos命令

8.后来找到:

How to just call a command and not get its output

好像说的是所需要的:

p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

out, err = p.communicate()

# do something with out, err, or don't bother altogether.

有空可以去试试。

但是我试了试其所说的:

>>> subprocess.call('ping 127.0.0.1')

Pinging 127.0.0.1 with 32 bytes of data:

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.0.0.1:

Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

Approximate round trip times in milli-seconds:

Minimum = 0ms, Maximum = 0ms, Average = 0ms

0

>>> subprocess.call('ping 127.0.0.1', stdout=subprocess.PIPE)

0

用的是IDLE,结果都还是会弹出cmd窗口的。

9.另外这个:

How to execute a command prompt command from python

也可以参考看看。

【总结】

有空再深究。暂且到此为止。

python 隐藏命令行窗口_python如何只执行cmd中的动作,但消除或隐藏cmd窗口 - 小众知识...相关推荐

  1. python编写命令行框架_python的pytest框架之命令行参数详解(上)

    前言 pytest是一款强大的python自动化测试工具,可以胜任各种类型或者级别的软件测试工作.pytest提供了丰富的功能,包括assert重写,第三方插件,以及其他测试工具无法比拟的fixtur ...

  2. python从命令行获取参数_python从命令行获取参数操作

    python从命令行获取参数 一: 简单用法 从命令行获取参数两种方式: 1.通过sys.argv参数获取:代码如下: # -*- coding: utf-8 -*- import sys #获取输入 ...

  3. python命令行解析_python命令行解析函数

    sys.argv 在终端运行python 1.py hahah importsysprint(sys.argv) #['1.py', 'hahah'] argparse Python的命令行解析模块, ...

  4. mysql隐藏密码_MySQL在Linux系统中隐藏命令行中的密码的方法

    在命令行中输入命令并不是一个好主意,会造成安全问题.但是如果你决定去写一个应用,而这个应用需要在命令行中使用密码或者其他敏感信息.那么,你能通过以下方法禁止系统的其他用户轻易的看到这些敏感数据 呢?, ...

  5. python argparse_Python 命令行之旅:初探 argparse

    本文首发于 HelloGitHub 公众号,并发表于 Prodesire 博客. 前言 你是否好奇过在命令行中敲入一段命令后,它是如何被解析执行的?是否考虑过由自己实现一个命令行工具,帮你执行和处理任 ...

  6. python工具是什么-使用Python编写命令行工具有什么好的库?

    使用Python编写命令行工具的库很多,我最推荐的还是Google Fire Hello World 要介绍Fire是什么,看一个简单的例子就明白了 # calc.py import fire cla ...

  7. python脚本实例手机端-python链接手机用Python实现命令行闹钟脚本实例

    前言: 这篇文章给大家介绍了怎样用python创建一个简单的报警,它可以运行在命令行终端,它需要分钟做为命令行参数,在这个分钟后会打印"wake-up"消息,并响铃报警,你可以用0 ...

  8. python argparse_Python 命令行之旅:argparse、docopt、click 和 fire 总结篇

    本文首发于HelloGitHub公众号,并发表于Prodesire 博客. 一.前言 在近半年的 Python 命令行旅程中,我们依次学习了 argparse.docopt.click 和 fire ...

  9. python argparse_Python 命令行之旅——初探 argparse

    『讲解开源项目系列』启动--让对开源项目感兴趣的人不再畏惧.让开源项目的发起者不再孤单.跟着我们的文章,你会发现编程的乐趣.使用和发现参与开源项目如此简单.欢迎联系我们给我们投稿,让更多人爱上开源.贡 ...

最新文章

  1. Java基础(二十七)Java IO(4)字符流(Character Stream)
  2. 中科院博导: 读博士感到痛苦的五个原因
  3. duilib 显示内存图片
  4. Qt关闭程序的时候创建json文件,再次启动时重新加载到界面上
  5. 雷鸟邮件查找所有星标邮件_雷鸟的4种轻量级电子邮件替代品
  6. 电击图片弹出无边自定义窗口
  7. java setcharat,Java StringBuilder setCharAt()方法
  8. Java高手速成│编写你第一个数据库程序
  9. [转载] 【汇总】Android知识清单
  10. excel替换快捷键_excel怎样查找替换 excel查找替换快捷键,看完你学会了么
  11. iptables 实现主机防火墙(四表五链)
  12. 计算机毕业设计之java+jsp517报刊图书征订管理系统
  13. WPF使用Blend
  14. 乐行学院Redis5学习教程 第一章redis5的安装
  15. 终于有人把 单点 登录说清楚了!
  16. 学习python不要盲目跟风,看看自己适合吗?
  17. 教师资格证科目二客观题汇总
  18. python中整数的长度_Python中正整数的位长度
  19. 大数据之------------数据中台
  20. Java大数据基础——day01

热门文章

  1. 父亲的忠告:把孩子培养成普通人
  2. 如何处理Global symbol * requires explicit package name编译错误,以及use strict用法
  3. 管理信息系统数据库设计标准(草稿)
  4. 初一辍学学php能行吗_《夺冠》破7亿,辍学的农村姑娘成排球女王,朱婷:百炼才能成钢...
  5. Java_WEB项目OOM(OutOfMemError内存溢出) MyEclipse配置Jvm内存
  6. adb 启动命令,pc启动两个微信,INSTALL_FAILED_CONFLICTING_PROVIDER
  7. Ant Design Pro 开发上手
  8. react元素显隐控制
  9. linkedhashmap 顺序_LinkedHashMap 源码详细分析(JDK1.8)
  10. 16 | 二分查找(下):如何快速定位IP对应的省份地址?