日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

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

發(fā)布時(shí)間:2023/12/10 python 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 隐藏命令行窗口_python如何只执行cmd中的动作,但消除或隐藏cmd窗口 - 小众知识... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

【問題】

這里提到的,打包python中,由于python中調(diào)用windows的cmd去執(zhí)行一些動(dòng)作,所以打包后的python,結(jié)果還是會(huì)遇到,調(diào)用cmd窗口(執(zhí)行了對(duì)應(yīng)的命令后)一閃而過。

想要消除此cmd一閃而過的問題。

只希望運(yùn)行命令,執(zhí)行動(dòng)作。徹底不希望看到cmd的窗口顯示出來(lái)。

【解決過程】

1.網(wǎng)上搜了搜,是關(guān)于python調(diào)用cmd的一些內(nèi)容,所以說(shuō)了:

設(shè)置shell為false,類似于:

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

3.或者:

給cmd.exe 添加/Q參數(shù),類似于:

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

但是結(jié)果不行:

有朝這方向想過,上面的我試過不行

shell默認(rèn)為False,為真的話,unix下相當(dāng)于args前面添加了 "/bin/sh" "-c",window下,相當(dāng)于添加"cmd.exe /c",和隱藏shell窗口沒關(guān)系吧

2.也去看了看,cmd本身的一些參數(shù)支持:

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命令控制臺(tái)窗口一閃而過運(yùn)行后就消失的方法

結(jié)果根本不是想要的.

4.參考這里,好像是可以通過最開始先用

?

1

@echo off

然后再加上要運(yùn)行的命令,好像就可以不顯示輸出了。

但是貌似這和python中調(diào)用cmd,也還是不一樣。

不過有機(jī)會(huì)可以去試試。。。

5.不過剛又注意到,其實(shí)cmd的:

/Q Turns echo off

就應(yīng)該等價(jià)于上述的

@echo off

才對(duì)。

6.這里提到了三種方式:

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

CreateProcess

隱藏cmd,并用管道實(shí)現(xiàn)命令運(yùn)行

WinExec有時(shí)候好像不能運(yùn)行,不知道為什么。

CreateProcess 第二個(gè)參數(shù)直接寫入命令就行了,第一個(gè)留空(NULL),。后面屬性里面加隱藏。

管道最麻煩,不過用起來(lái)交互是最好的。

但是不知道如何操作。

7.找到關(guān)于CreateProcess的示例了:

隱藏cmd窗口執(zhí)行dos命令

8.后來(lái)找到:

How to just call a command and not get its output

好像說(shuō)的是所需要的:

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

out, err = p.communicate()

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

有空可以去試試。

但是我試了試其所說(shuō)的:

>>> 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,結(jié)果都還是會(huì)彈出cmd窗口的。

9.另外這個(gè):

How to execute a command prompt command from python

也可以參考看看。

【總結(jié)】

有空再深究。暫且到此為止。

總結(jié)

以上是生活随笔為你收集整理的python 隐藏命令行窗口_python如何只执行cmd中的动作,但消除或隐藏cmd窗口 - 小众知识...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。