[英]python script to open windows command prompt and print some strings
I have a scenario where I have 3 python script files . 我有一个场景,其中有3个python脚本文件 。 Using one file I will create a text file. 使用一个文件,我将创建一个文本文件。 Using the other two script files, I will append some strings to the file which I have created earlier. 使用其他两个脚本文件,我会将一些字符串附加到之前创建的文件中。 So far, it's working good. 到目前为止,一切正常。
But, I need to do the same in a different way now. 但是,我现在需要以不同的方式做同样的事情。 I need to open a command prompt , then print the strings on that command prompt, using two python script files. 我需要打开一个命令提示符 ,然后使用两个python脚本文件在该命令提示符上打印字符串。
Can anyone please help? 谁能帮忙吗?
Thanks in Advance. 提前致谢。
I'm guessing that you're using windows so I'll attempt to answer your question with that assumption. 我猜您正在使用Windows,因此我将尝试以这种假设回答您的问题。 If I understand what you're trying to accomplish you have 1 script to launch the command prompt, then you want to launch the other 2 scripts and redirect output to the command line. 如果我了解您要完成的工作,那么您有1个脚本来启动命令提示符,那么您想启动其他2个脚本并将输出重定向到命令行。 Because you're really light on the details all I can give you is some pretty generic stuff with the easy way being something like this: 因为您真的很了解细节,所以我只能为您提供一些非常通用的东西,简单的方法是这样的:
import subprocess
# Opens cmd.exe and pipes stdout
p1 = subprocess.Popen('cmd', shell=True, stdin=None, stdout=subprocess.PIPE)
# Runs python scripts using stdin and stdout from p1
p2 = subprocess.Popen('python.exe foo.py', shell=True, stdin=p1.stdout)
p3 = subprocess.Popen('python.exe bar.py', shell=True, stdin=p1.stdout)
p1.stdout.close()
p2.communicate()
p3.communicate()
Another option would be to allow arguments so you would run the initial script like this: 另一个选择是允许自变量,这样您就可以像这样运行初始脚本:
python main.py foo.py bar.py
Without a more specific question that's about as good an answer as I can come up with. 如果没有更具体的问题,那将是我能想到的最佳答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.