【问题】
这里提到的,打包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] C:\Users\CLi>cmd /? CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF] /C Carries out the command specified by string and then terminates /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 ‘&&’ 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 used to process quote (") characters: 1. If all of the following conditions are met, then quote characters – no /S switch – 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 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 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 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 registry settings. In a batch file, the SETLOCAL ENABLEEXTENSIONS or DISABLEEXTENSIONS arguments The command extensions involve changes and/or additions to the following DEL or ERASE 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 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 registry settings. In a batch file the SETLOCAL ENABLEDELAYEDEXPANSION or DISABLEDELAYEDEXPANSION for details. If delayed environment variable expansion is enabled, then the exclamation at execution time. You can enable or disable file name completion for a particular 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 and/or HKEY_CURRENT_USER\Software\Microsoft\Command Processor\CompletionChar with the hex value of a control character to use for a particular 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 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 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 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一闪而过
找到一些参考资料:
结果根本不是想要的.
4.参考这里,好像是可以通过最开始先用
@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的示例了:
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.
有空可以去试试。
但是我试了试其所说的:
|
用的是IDLE,结果都还是会弹出cmd窗口的。
9.另外这个:
How to execute a command prompt command from python
也可以参考看看。
【总结】
有空再深究。暂且到此为止。
转载请注明:在路上 » 【也许解决】如何只执行cmd中的动作,但消除或隐藏cmd窗口,即不显示cmd窗口,连cmd的一闪而过都不希望看到