Tag: subprocess

来自subprocess命令的实时输出

我使用Python脚本作为stream体动力学代码的驱动程序。 当运行模拟时,我使用subprocess.Popen来运行代码,将stdout和stderr的输出收集到subprocess.PIPE —然后我可以打印(并保存到日志文件)输出信息,并检查是否有错误。 问题是,我不知道代码是如何进展的。 如果我直接从命令行运行它,它会给出关于它在什么时候迭代的输出,什么时间,什么是下一个时间步,等等。 有没有一种方法来存储输出(用于logging和错误检查),并且还产生实时stream输出? 我的代码的相关部分: ret_val = subprocess.Popen( run_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True ) output, errors = ret_val.communicate() log_file.write(output) print output if( ret_val.returncode ): print "RUN failed\n\n%s\n\n" % (errors) success = False if( errors ): log_file.write("\n\n%s\n\n" % errors) 本来我是通过teepipe道run_command ,以便副本直接到日志文件,并仍然输出stream到terminal – 但这样我不能存储任何错误(我知道)。 编辑: 临时解决scheme: ret_val = subprocess.Popen( run_command, stdout=log_file, stderr=subprocess.PIPE, shell=True ) while not […]

用Python脚本使用sudo

我试图写一个小脚本来挂载一个VirtualBox共享文件夹,每次我执行脚本。 我想用Python来做,因为我正在尝试学习脚本。 问题是我需要启动mount命令的权限。 我可以运行脚本作为sudo,但我更喜欢它自己做sudo。 我已经知道把你的密码写入一个.py文件是不安全的,但是我们正在谈论的是一个并不重要的虚拟机:我只是想点击.py脚本来让它工作。 这是我的尝试: #!/usr/bin/env python import subprocess sudoPassword = 'mypass' command = 'mount -t vboxsf myfolder /home/myuser/myfolder' subprocess.Popen('sudo -S' , shell=True,stdout=subprocess.PIPE) subprocess.Popen(sudoPassword , shell=True,stdout=subprocess.PIPE) subprocess.Popen(command , shell=True,stdout=subprocess.PIPE) 我的Python版本是2.6

显示子过程输出到标准输出并redirect

我正在通过Python的subprocess模块运行一个脚本。 目前我使用: p = subprocess.Popen('/path/to/script', stdout=subprocess.PIPE, stderr=subprocess.PIPE) result = p.communicate() 然后我将结果打印到标准输出。 这一切都很好,但由于脚本需要很长时间才能完成,所以我希望从脚本实时输出到stdout。 我输出输出的原因是因为我想parsing它。

Python:并行执行catsubprocess

我正在运行几个cat | zgrep 在远程服务器上执行cat | zgrep命令并单独收集输出以进一步处理: class MainProcessor(mp.Process): def __init__(self, peaks_array): super(MainProcessor, self).__init__() self.peaks_array = peaks_array def run(self): for peak_arr in self.peaks_array: peak_processor = PeakProcessor(peak_arr) peak_processor.start() class PeakProcessor(mp.Process): def __init__(self, peak_arr): super(PeakProcessor, self).__init__() self.peak_arr = peak_arr def run(self): command = 'ssh remote_host cat files_to_process | zgrep –mmap "regex" ' log_lines = (subprocess.check_output(command, shell=True)).split('\n') process_data(log_lines) 但是,这会导致顺序执行subprocess('ssh […]

Python:如何使用subprocessredirect输出?

我在命令行中做了什么: cat file1 file2 file3 > myfile 我想用python做什么: import subprocess, shlex my_cmd = 'cat file1 file2 file3 > myfile' args = shlex.split(my_cmd) subprocess.call(args) # spits the output in the window i call my python program

使用subprocess获取实时输出

我正在尝试编写一个命令行程序的包装脚本(svnadminvalidation),将显示一个很好的进度指示器的操作。 这要求我能够在输出包装程序后立即看到每行输出。 我想我只是使用subprocess.Popen执行程序,使用stdout=PIPE ,然后读取每一行,并相应地采取行动。 但是,当我运行下面的代码时,输​​出似乎被缓冲在某处,导致它出现在两个块,第1行到第332行,然后是第333到第439行(输出的最后一行) from subprocess import Popen, PIPE, STDOUT p = Popen('svnadmin verify /var/svn/repos/config', stdout = PIPE, stderr = STDOUT, shell = True) for line in p.stdout: print line.replace('\n', '') 在稍微查看Popen的文档后,我发现了bufsize参数,所以我尝试将bufsize设置为1(缓冲每行)和0(无缓冲),但是这两个值都没有改变线路传送的方式。 在这一点上,我开始抓住吸pipe,所以我写了下面的输出循环: while True: try: print p.stdout.next().replace('\n', '') except StopIteration: break 但得到了同样的结果。 是否有可能获得使用subprocess执行的程序的“实时”程序输出? Python中是否有其他的选项是向前兼容的(而不是exec* )?

Python线程多个bashsubprocess?

如何使用线程和subprocess模块来产生并行的bash进程? 当我开始线程ala第一个答案在这里: 如何在Python中使用线程? ,bash进程顺序运行而不是并行运行。

当我使用os.system()或subprocess.call()时如何隐藏控制台?

我写了一些如下所示的语句: os.system(cmd) #do something subprocess.call('taskkill /F /IM exename.exe') 都会popup一个控制台。 我怎样才能阻止它popup控制台?

停止阅读Python的过程输出没有挂起?

我有一个Linux的Python程序几乎看起来像这样: import os import time process = os.popen("top").readlines() time.sleep(1) os.popen("killall top") print process 该程序挂在这一行: process = os.popen("top").readlines() 并发生在保持更新输出的工具,如“顶” 我最好的尝试: import os import time import subprocess process = subprocess.Popen('top') time.sleep(2) os.popen("killall top") print process 它比第一个(它是凯尔特人)更好,但它返回: <subprocess.Popen object at 0x97a50cc> 第二次审判: import os import time import subprocess process = subprocess.Popen('top').readlines() time.sleep(2) os.popen("killall top") print process 与第一个相同。 它由于“readlines()”而被吊死 […]

逐行读取subprocessstdout

我的Python脚本使用subprocess调用非常嘈杂的Linux实用程序。 我想将所有的输出存储到一个日志文件,并显示给用户。 我认为以下方法可行,但是在应用程序产生大量输出之前,输出不会显示在我的应用程序中。 #fake_utility.py, just generates lots of output over time import time i = 0 while True: print hex(i)*512 i += 1 time.sleep(0.5) #filters output import subprocess proc = subprocess.Popen(['python','fake_utility.py'],stdout=subprocess.PIPE) for line in proc.stdout: #the real code does filtering here print "test:", line.rstrip() 我真正想要的行为是filter脚本打印从subprocess收到的每一行。 Sorta像什么tee ,但与Python代码。 我错过了什么? 这甚至有可能吗? 更新: 如果将一个sys.stdout.flush()添加到fake_utility.py中,则代码在python 3.1中具有所需的行为。 我使用Python 2.6。 你会认为使用proc.stdout.xreadlines()会像py3k一样工作,但是不会。 […]