简体   繁体   English

Python 3的命令行

[英]Command line from Python 3

I have 1 python 3 script. 我有1个python 3脚本。 I need to use another script via command line. 我需要通过命令行使用另一个脚本。 What function should i use? 我应该使用什么功能? I mean something like that: 我的意思是这样的:

res = execute('C:\python32\python Z:\home\192.168.0.15\www\start.pyw start=1 module=server > Z:\home\192.168.0.15\www\test.html')

Check out the Process Management section of the os module 查看os模块的Process Management部分

http://docs.python.org/3/library/os.html#module-os http://docs.python.org/3/library/os.html#module-os

os.popen will work well if you are interested in i/o with the process 如果您对该过程感兴趣,则os.popen可以很好地工作

Use the subprocess module. 使用子流程模块。 That gives you the most flexibility. 这给您最大的灵活性。

This is a python program you want to start. 这是您要启动的python程序。 It would be better to import the module, run the method you want and write the output to a file. 最好导入模块,运行所需的方法,然后将输出写入文件。

However, this would be how you can do it via shell execution: 但是,这将是您可以通过shell执行的方法:

from subprocess import *
command_stdout = Popen(['C:\python32\python', 'Z:\home\192.168.0.15\www\start.pyw', 'start=1', 'module=server'], stdout=PIPE).communicate()[0]
res = command_stdout.decode("utf-8")
fd = open('Z:\home\192.168.0.15\www\test.html',"w")
fd.write(res)

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

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