我想运行一个python脚本并捕获文本文件的输出以及想要在控制台上显示.
我想将它指定为python脚本本身的属性.不要echo "hello world" | tee test.txt每次都在命令提示符上使用该命令.
在脚本中我试过:
sys.stdout = open('log.txt','w')
Run Code Online (Sandbox Code Playgroud)
但是这并没有在屏幕上显示stdout输出.
我听说过记录模块,但是我无法使用该模块来完成这项工作.
我发现使用paramiko很难在后台运行一个进程.我用了 :
stdin, stdout, stderr = ssh.exec_command('executefile.py &')
Run Code Online (Sandbox Code Playgroud)
并发现没有发现executefile.py进程正在运行.
然后我尝试使用其他方式包括反斜杠:
stdin, stdout, stderr = ssh.exec_command('executefile.py \&')
Run Code Online (Sandbox Code Playgroud)
这种方法奏效了.有一个实例在机器上运行,但毫不奇怪,它没有在后台运行.我可以知道因为它没有在后台运行,因为代码在此代码之后停留在第二行.它是
all_inf = stdout.readlines()
Run Code Online (Sandbox Code Playgroud)
现在代码不会超出上面的行,除非脚本的进程被杀死.
我正在学习paramiko,任何帮助表示赞赏.
我已经安装了python 2.7并尝试运行下面的代码总是会出错:
# !/usr/bin/python
import os, sys
stinfo = os.statvfs("C:\Tools")
Run Code Online (Sandbox Code Playgroud)
错误来自:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'statvfs'
Run Code Online (Sandbox Code Playgroud)
有什么线索让它起作用?
我有一个程序在运行时解析输出,比较从给定值的时间和打印差异,但它不按我的方式工作:
a = 'Time Taken for Response: 31 msec'
time_out = 75
if a.split()[4] > time_out:
print "time taken is more than given conditions"
print a.split()[4]
Run Code Online (Sandbox Code Playgroud)
输出如下:
time taken is more than given conditions
31
Run Code Online (Sandbox Code Playgroud)
我不明白为什么程序进入循环本身时 31 < 75
任何线索或指导???