在我的代码中,我有一个类似于这样的行:
rval = subprocess.call(["mkdir",directoryName], shell=True)
Run Code Online (Sandbox Code Playgroud)
我可以检查rval它是否是,0或者1如果它是1,我想以"A subdirectory or file ben already exists."文件格式从命令中获取文本,所以如果我想确保文本是相同的,我可以将它与另一个文件进行比较.
是否有可能有这样的线,但我知道这不起作用
rval = subprocess.call(["mkdir",directoryName], shell=True) >> filename
Run Code Online (Sandbox Code Playgroud)
所以无论命令发生了什么,文本都被捕获filename,并且rval仍然有返回码?
我想尽快重复执行一个子进程.但是,有时这个过程需要很长时间,所以我想杀掉它.我使用signal.signal(...)如下所示:
ppid=pipeexe.pid
signal.signal(signal.SIGALRM, stop_handler)
signal.alarm(1)
.....
def stop_handler(signal, frame):
print 'Stop test'+testdir+'for time out'
if(pipeexe.poll()==None and hasattr(signal, "SIGKILL")):
os.kill(ppid, signal.SIGKILL)
return False
Run Code Online (Sandbox Code Playgroud)
但有时这段代码会试图阻止下一轮执行.停止测试/ home/lu/workspace/152/treefit/test2for time out/bin/sh:/ home/lu/workspace/153/squib_driver:not found ---这是下一次执行; 该程序错误地阻止它.
有谁知道如何解决这个问题?我想及时停止不执行1秒钟.sleep(n)经常等待n秒.我不希望我希望它可以执行不到1秒
我正在尝试制作一个有按钮的PyGtk Gui.当用户按下此按钮时,gnome-terminal提示用户输入密码.
然后它将为JQuery片段克隆这个Git存储库gedit.
然后,它将js.xml文件复制到/usr/share/gedit/plugins/snippets/js.xml
最后,它强制删除了Git存储库.
命令:
gnome-terminal -x sudo git clone git://github.com/pererinha/gedit-snippet-jquery.git && sudo cp -f gedit-snippet-jquery/js.xml /usr/share/gedit/plugins/snippets/js.xml && sudo rm -rf gedit-snippet-jquery
Run Code Online (Sandbox Code Playgroud)
它在我的终端工作正常.
但是,通过它刚打开的GUI,我添加了我的密码,按回车键,然后再次关闭.
我只想将命令运行到第一个命令 &&
这是我的Python函数(带命令):
def on_install_jquery_code_snippet_for_gedit_activate(self, widget):
""" Install Jquery code snippet for Gedit. """
cmd="gnome-terminal -x sudo git clone git://github.com/pererinha/gedit-snippet-jquery.git && sudo cp -f gedit-snippet-jquery/js.xml /usr/share/gedit/plugins/snippets/js.xml && sudo rm -rf gedit-snippet-jquery"
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT,
close_fds=False)
self.status.set_text(p.stdout.read()) #show response in 'status
Run Code Online (Sandbox Code Playgroud) 在安装了32位python 2.7的64位系统中,我尝试执行以下操作:
import subprocess
p = subprocess.call('dir', shell=True)
print p
Run Code Online (Sandbox Code Playgroud)
但这给了我:
Traceback (most recent call last):
File "test.py", line 2, in <module>
p = subprocess.call('dir', shell=True)
File "C:\Python27\lib\subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 709, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 957, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Run Code Online (Sandbox Code Playgroud)
如果我在终端做...
dir
Run Code Online (Sandbox Code Playgroud)
...当然会打印当前文件夹内容.
我试图将shell参数更改为shell = False.
编辑:其实我不能在路径上调用任何可执行文件subprocess.call().该声明p = subprocess.call('dir', shell=True)在另一台机器上工作正常,我认为它是相关的.
如果我做
subprocess.call('PATH', shell=True)
Run Code Online (Sandbox Code Playgroud)
然后我明白了
Traceback (most …Run Code Online (Sandbox Code Playgroud) 我的python脚本(python 3.4.3)通过子进程调用bash脚本:
import subprocess as sp
res = sp.check_output("bashscript", shell=True)
Run Code Online (Sandbox Code Playgroud)
该bashscript包含以下行:
ssh -MNf somehost
Run Code Online (Sandbox Code Playgroud)
这将打开与某个远程主机的共享主连接,以允许一些后续操作.
执行python脚本时,它将提示该ssh行的密码,但在输入密码后它会阻塞,并且永远不会返回.当我按ctrl-C终止脚本时,我看到连接已正确建立(因此ssh行已成功执行).
使用时,我没有这个阻塞问题check_call,而不是check_output,但check_call不检索标准输出.我想了解究竟是什么导致阻塞行为check_output,可能与某些微妙的相关ssh -MNf.
让我们考虑一下这个片段:
from subprocess import Popen, PIPE, CalledProcessError
def execute(cmd):
with Popen(cmd, shell=True, stdout=PIPE, bufsize=1, universal_newlines=True) as p:
for line in p.stdout:
print(line, end='')
if p.returncode != 0:
raise CalledProcessError(p.returncode, p.args)
base_cmd = [
"cmd", "/c", "d:\\virtual_envs\\py362_32\\Scripts\\activate",
"&&"
]
cmd1 = " ".join(base_cmd + ['python -c "import sys; print(sys.version)"'])
cmd2 = " ".join(base_cmd + ["python -m http.server"])
Run Code Online (Sandbox Code Playgroud)
如果我运行execute(cmd1)输出将打印没有任何问题.
但是,如果我运行execute(cmd2)而不会打印任何内容,为什么会这样,我如何修复它,以便我可以实时看到http.server的输出.
另外,如何for line in p.stdout在内部进行评估?是什么样的无限循环,直到达到stoout eof或什么?
这个主题在SO中已经解决了几次,但我还没有找到一个Windows解决方案.上面的片段实际上是来自这个答案的代码并尝试从virtualenv运行http.server(在win7上运行python3.6.2-32bits)
我试图用几个参数通过Python调用一个进程.执行批处理文件本身对我来说很好,但将其翻译成Python会让我尖叫.这里是批处理文件的内容:
"C:\Program Files\bin\cspybat" "C:\Program Files\bin\armproc.dll" "C:\Program Files\bin\armjlink.dll" "C:\Documents and Settings\USER\Desktop\CAL\testing\Verification\FRT\Code\TC1\Output\Genericb\Debug\Exe\Gen.out" --download_only --backend -B "--endian=little" "--cpu=Cortex-M3" "--fpu=None" "-p" "C:\Program Files\CONFIG\debugger\ST\iostm32f10xxb.ddf" "--drv_verify_download" "--semihosting" "--device=STM32F10xxB" "-d" "jlink" "--drv_communication=USB0" "--jlink_speed=auto" "--jlink_initial_speed=32" "--jlink_reset_strategy=0,0"
Run Code Online (Sandbox Code Playgroud)
批处理文件运行的可执行文件已命名cspybat.可执行文件的输出提供了以下信息:-- All parameters afterbackend are passed to the back end.
另请注意,有些参数是字符串,有些则不是.
解
这对我有用:
""" MCU flashing function"""
params = [r"C:\Program Files\bin\cspy",
r"C:\Program Files\bin\arpro.dll",
r"C:\Program Files\bin\arjink.dll",
r"C:\Documents and Settings\USER\Desktop\Exe\GenerV530b.out",
"--download_only", "--backend", "-B", "--endian=little", "--cpu=Cort3", "--fpu=None", "-p",
r"C:\Program Files\CONFIG\debugger\ST\iostm32f10xxb.ddf",
"--drv_verify_download", "--semihosting", "--device=STM32F10xxB", "-d", "jlink", "--drv_communication=USB0",
"--jlink_speed=auto", "--jlink_initial_speed=32", "--jlink_reset_strategy=0,0" ]
print(subprocess.list2cmdline(params)) …Run Code Online (Sandbox Code Playgroud) 我正在运行一个脚本,通过使用执行许多可执行文件
subprocess.call(cmdArgs,stdout=outf, stderr=errf)
Run Code Online (Sandbox Code Playgroud)
when outf/ errf是None或文件描述符(stdout/的不同文件stderr).
有什么方法可以执行每个exe,以便将stdout和stderr一起写入文件和终端?
我在Raspberry Pi上,我正在使用一个叫做的程序fswebcam,它允许你用网络摄像头拍照.
~$ fswebcam image.jpg
Run Code Online (Sandbox Code Playgroud)
如果在终端输入该命令拍摄照片并将其保存到您的计算机,但是我想构建一个简单的python程序,它可以访问终端并执行与上面列出的相同的命令.
我试过import os并使用os.system('fswebcam image.jpg')但它不适合我.
我怎么能有python执行终端命令?
我正在使用子进程从Python(3.5.2)脚本运行命令行程序,我在Jupyter笔记本中运行该脚本.子进程需要很长时间才能运行,因此我希望它的stdout能够在Jupyter笔记本中实时打印到屏幕上.
我可以在从终端运行的普通Python脚本中做到这一点没问题.我这样做使用:
def run_command(cmd):
from subprocess import Popen, PIPE
import shlex
with Popen(shlex.split(cmd), stdout=PIPE, bufsize=1, universal_newlines=True) as p:
for line in p.stdout:
print(line, end='')
exit_code = p.poll()
return exit_code
Run Code Online (Sandbox Code Playgroud)
但是,当我在Jupyter笔记本中运行脚本时,它不会将stdout实时打印到屏幕上.相反,它会在子进程运行完毕后打印所有内容.
有没有人对如何解决这个问题有任何想法?
非常感谢,约翰尼
python ×10
subprocess ×10
popen ×2
bash ×1
ipython ×1
jupyter ×1
kill ×1
parent ×1
path ×1
pygtk ×1
python-2.7 ×1
python-3.x ×1
raspberry-pi ×1
raspbian ×1
shell ×1
signals ×1
ssh ×1
stdout ×1
sublimetext3 ×1
timeout ×1
windows ×1