简体   繁体   English

python脚本打开Windows命令提示符并打印一些字符串

[英]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.

相关问题 在同一打开的窗口中的python打印命令不在提示符下 - python print command in same open window not in prompt 在特定位置打开命令提示符,然后使用 python 在该位置运行带有一些命令行参数的脚本 - Open a command prompt at a specific location followed by running a script at that location with some command line arguments using python Python:无法使用Python脚本打开新的命令提示符会话并通过命令行(Windows)运行程序 - Python: Unable to open a new command prompt session and run a program through command line (windows) using Python script Python:从python脚本的Windows命令提示符中执行windows命令 - Python: Executing the windows command in windows command prompt from python script 如何打开命令提示符以及使用python脚本运行的命令? - How to open a command prompt along with a command to run using a python script? 重新启动脚本后命令提示符(Windows)和 python 脚本出现问题 - Issue with command prompt (windows) and python script after restarting script 使用Windows命令提示符查找python脚本的Windows PID - Find Windows PID of a python script with Windows Command Prompt 如何在 Windows 命令提示符中打印 Python 单元测试覆盖率报告? - How to print Python unit tests coverage report in Windows Command Prompt? Python脚本无法在命令提示符下打印输出 - Python script doesn't print output in command prompt 如何在命令提示符Windows 10中运行python脚本 - How to run python script in Command Prompt Windows 10
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM