python对popen.communicate的调用不提供任何输出
我有一个类函数声明为python对popen.communicate的调用不提供任何输出,python,Python,我有一个类函数声明为 def catFunc(self,filename): print "catFunc",filename process = subprocess.Popen(['cat',/root/scratch.php], stdout=subprocess.PIPE, shell=True) out, err = process.communicate() print (out) print (err) 就这样叫 fn = '/ro
def catFunc(self,filename):
print "catFunc",filename
process = subprocess.Popen(['cat',/root/scratch.php], stdout=subprocess.PIPE, shell=True)
out, err = process.communicate()
print (out)
print (err)
就这样叫
fn = '/root/scratch.php'
self.catFunc(fn)
但我看不到任何结果,也不知道为什么
非常感谢您对阅读文件内容的任何帮助
def catFunc(self,filename):
with open(filename) as f:
s = f.read()
return s
如果需要使用子流程模块:
import subprocess
def catFunc(filename):
print "catFunc"
task = subprocess.Popen(["cat", filename], stdout=subprocess.PIPE)
print list(task.stdout)
catFunc()
读取文件内容的步骤
def catFunc(self,filename):
with open(filename) as f:
s = f.read()
return s
如果需要使用子流程模块:
import subprocess
def catFunc(filename):
print "catFunc"
task = subprocess.Popen(["cat", filename], stdout=subprocess.PIPE)
print list(task.stdout)
catFunc()
要检索shell命令的输出,建议使用
子进程。与shlex.split
合作检查\u output
例如:
output = subprocess.check_output(shlex.split('cat "root/scratch.php"')
也就是说,根据当前的问题标题,Rakesh是正确的。要检索shell命令的输出,建议使用
子进程。与shlex.split
合作检查输出
例如:
output = subprocess.check_output(shlex.split('cat "root/scratch.php"')
也就是说,根据当前的问题标题,Rakesh是正确的。我知道我可以打点它-我的目的是解除popen.Communication功能我知道我可以打点它-我的目的是解除popen.Communication功能可能流程没有?或者更好的方法是通过打印其进程来检查Popen是否失败。returncode
。返回代码为none可能进程没有?或者最好通过打印其进程来检查Popen是否失败。returncode
。返回代码为None