简体   繁体   English

如何避免弹出cmd?

[英]How to avoid popup cmd?

I am writing a program which has to call another program.我正在编写一个必须调用另一个程序的程序。 So I've done some research and found the os module.所以我做了一些研究并找到了 os 模块。 The problem with this solution is that the cmd always pops up.这个解决方案的问题是 cmd 总是弹出。 How can I avoid this?我怎样才能避免这种情况?

So far I used the command like this: os.system("something.py")到目前为止,我使用了这样的命令: os.system("something.py")

What you are looking for is probably found in the subprocess module.您正在寻找的内容可能在subprocess模块中找到。

Now there is not really any detail as to what you are trying to do when something.py runs, so lets say we have a file called test.py that creates a new file and writes something:现在没有关于当something.py运行时你想要做什么的任何细节,所以假设我们有一个名为test.py的文件,它创建一个新文件并写入一些内容:

test.py测试文件

t = r"C:\Users\Paul\Desktop\test.txt"

with open(t, 'w') as f:
    f.write("This worked!")

So in whatever program we want to run test.py , we would have the following:所以在我们想要运行test.py任何程序中,我们都会有以下内容:

import subprocess
f = r"C:\Users\Paul\Desktop\test.py"
subprocess.run(f, shell=True) # shell=True since this is really an arg

And the program will execute without opening a terminal.并且程序将在不打开终端的情况下执行。 See here:看这里:

>>> import subprocess
>>> f = r"C:\Users\Paul\Desktop\test.py"
>>> subprocess.run(f, shell=True)
CompletedProcess(args='C:\\Users\\Paul\\Desktop\\test.py', returncode=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM