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

【问题】

这里提到的,打包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

也可以参考看看。

【总结】

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/428204.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

world文档粘贴图片进去看不到

在大学做实验报告的时候经常要插入一些截图&#xff0c;往往会遇到直接复制粘贴图片会看不到的情况&#xff0c;会很烦&#xff0c;我自己也找了好多方法&#xff0c;下面是我最喜欢的简便方法的一种。 解决方法如下: 可以点击鼠标右键选择段落&#xff0c;将行距修改为1.5倍即…

Ajax 调用webservice 解决跨域请求和发布到服务器后本地调用成功外网失败的问题...

webservice 代码 1 /// <summary>2 /// MESService 的摘要说明3 /// </summary>4 [WebService(Namespace "http://tempuri.org/")]5 [WebServiceBinding(ConformsTo WsiProfiles.BasicProfile1_1)]6 //[System.ComponentModel…

c盘users的用户名怎么改_怎么修改iPhone备份文件夹路径 iPhone C盘路径修改教程【详解】...

iPhone备份文件夹路径怎么修改_iPhone C盘备份路径修改教程 我们都知道iPhone默认的备份是在C盘&#xff0c;不过现在各种视频、照片体积那么大&#xff0c;小小的C盘只怕是负荷不了了&#xff0c;那么有什么办法去修改备份路径呢&#xff0c;下面小编就为大家介绍一下。需要注…

javaweb开发的准备工作——配置篇

1.配置 a. jdk配置&#xff08;用于web开发编程&#xff0c;此处不需要配置path&#xff0c;只需配置环境变量即可&#xff09; b. tomcat配置 打开Tomcat&#xff08;打开bin目录下的startup.bat文件&#xff09; 检验是否打开成功(两个网址都可以&#xff0c;出现汤姆猫即为…

阅读引擎开源项目调研总结

农历腊月初二&#xff0c;也是冬至后的第四个九天&#xff0c;俗称“四九”。冬至这一天开始数九&#xff0c;这就是人们所说的“提冬数九”。数上9天是一九&#xff0c;再数9天是二九……数到“九九”就算“九”尽了&#xff0c;“九尽杨花开”&#xff0c;那时天就暖了。《九…

ftp 上传文件夹_命令行连接FTP服务器

Windows下&#xff1a;打开命令行窗口&#xff0c;输入 ftp&#xff0c;进入ftp命令模式&#xff1a;输入 open ip地址 端口&#xff0c;进入ftp服务器&#xff0c;如open 172.16.3.77 2121。如下图&#xff1a;输入Windows下的用户名&#xff0c;然后输入密码&#xff08;注意…

创建实现一个简单的web项目

创建一个新的web项目 注意要选择Dynamic Web Project&#xff0c;第一次用可能要找一下&#xff0c;也可以直接搜索栏搜web&#xff0c;就会出来 图片是我自己照片照的&#xff0c;不好截图&#xff0c;看到有个人影勿慌哈哈哈哈&#xff0c;不是你眼睛的问题。 然后在web文件目…

一起学Python:列表介绍

列表介绍 想一想&#xff1a; 前面学习的字符串可以用来存储一串信息&#xff0c;那么想一想&#xff0c;怎样存储咱们班所有同学的名字呢&#xff1f;定义100个变量&#xff0c;每个变量存放一个学生的姓名可行吗&#xff1f;有更好的办法吗&#xff1f;答&#xff1a; 列表&l…

eclipse xml文件报错_Maven教程6: Maven与Eclipse整合

点击上方“Java技术前线”&#xff0c;选择“置顶或者星标”与你一起成长一、安装Maven插件下载下来的maven插件如下图所示&#xff1a;&#xff0c;插件存放的路径是&#xff1a;E:/MavenProject/Maven2EclipsePlugin进入到eclipse中的dropins目录下&#xff0c;新建三个txt文…

C语言#define宏定义可能注意不到的地方

#define使用的核心:直接替换 我也觉得自己很清楚这一点&#xff0c;但看到这一道输出程序片段结果题&#xff0c;还是懵了。大家也可以在不看我下方答案的情况下&#xff0c;自己做一下&#xff0c;题目如下: #include<stdio.h> #define P 3 #define S(a) P*a*a int mai…

结合JSP与HTML做一个九九乘法表

不说了&#xff0c;先上效果图&#xff0c;下面附有源码: 代码一:jsp表达式法 <% page language"java" contentType"text/html; charsetUTF-8"pageEncoding"UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitio…

python中parse是什么_Python中optparse模块使用浅析

最近遇到一个问题&#xff0c;是指定参数来运行某个特定的进程&#xff0c;这很类似Linux中一些命令的参数了&#xff0c;比如ls -a&#xff0c;为什么加上-a选项会响应。optparse模块实现的也是类似的功能&#xff0c;它是为脚本传递命令参数。 使用此模块前&#xff0c;首先需…

Topshelf创建Windows服务

入门例子 using log4net; using System; using System.Timers; using Topshelf;namespace ConsoleAppTest {class Program{static void Main(string[] args){log4net.Config.XmlConfigurator.Configure();HostFactory.Run(x >{x.Service<TownCrier>();x.RunAsLocalSys…

jsp网页中的换行

JSP网页中输出换行在一开始学习的时候困扰了我不短的时间&#xff0c;因为一些排版需要换行总是很难实现&#xff0c;随着学习&#xff0c;找到了如下几种方法做到换行&#xff0c;以保持页面的美观。 代码和截图我给放在下面了&#xff0c;附了注释: 代码如下&#xff0c;想…

npu算力如何计算_CPU、GPU、NPU、FPGA等芯片架构特点分析

来源 | 汽车电子与软件知圈 | 进“高精度地图社群”&#xff0c;请加微信15221054164&#xff0c;备注地图概述随着人工智能的热潮和AI算法的广泛应用&#xff0c;深度学习已成为当前AI研究的重点&#xff0c;在自动驾驶领域&#xff0c;环境感知、传感器融合、控制决策等等都会…

Eclipse——恢复的默认窗口设置篇

你可能也会像我一样&#xff0c;时不时不小心把一些窗口关掉了&#xff0c;然后想要改回来的时候&#xff0c;又要疯狂的去设置里把它们一个个的显示出来&#xff0c;大概率的还可能得百度一下&#xff0c;看着英文单词有时候就会烦躁。 直接崩溃&#xff1a; 这里一招教你直…

翻牌游戏如何打乱牌面java_家长专栏提高儿童记忆力的游戏训练

记忆是人脑对过去经验的保持和再现。记忆过程包括三个基本环节&#xff0c;即识记、保持、再认或回忆。识记是识别和记住事物。保持是将已获得的知识或经验巩固与保留在大脑中。再认是指过去经历过的事物再度出现时&#xff0c;能将它指认出来。回忆是指过去经历过的事物不在面…

keras中的mini-batch gradient descent (转)

深度学习的优化算法&#xff0c;说白了就是梯度下降。每次的参数更新有两种方式。 一、 第一种&#xff0c;遍历全部数据集算一次损失函数&#xff0c;然后算函数对各个参数的梯度&#xff0c;更新梯度。这种方法每更新一次参数都要把数据集里的所有样本都看一遍&#xff0c;计…

Leetcode 206. Reverse Linked List

Similar Questions Reverse Linked List II Binary Tree Upside Down Palindrome Linked List思路&#xff1a;链表反转。 解法一&#xff1a;迭代。 添加头节点&#xff08;推荐&#xff09;&#xff1a;不断将当前元素start插入dummy和dummy.next之间&#xff0c;实现反转。…

java中输出系统时间

老用老忘&#xff0c;乐此不疲 自己给自己写个模板&#xff0c;忘了随时copy也很香&#xff0c;不是吗&#xff1f; 不说废话了&#xff0c;上代码: package java操作的复习;import java.text.SimpleDateFormat; import java.util.Date;public class SystemTime {public sta…