Python 2.7子进程没有返回任何内容,exact命令在控制台中有效

时间:2016-06-27 20:52:34

标签: python python-2.7 console subprocess

我正在尝试在Python脚本中执行strings命令。我让它工作了一段时间,但神秘的是,当我再使用subprocess.Popen时,它不会返回任何内容。

从终端

调用
strings /path/to/.bashrc
# You may uncomment the following lines if you want `ls' to be colorized:
# export LS_OPTIONS='--color=auto'
# eval `dircolors`
# alias ls='ls $LS_OPTIONS'
# alias ll='ls $LS_OPTIONS -l'
# alias l='ls $LS_OPTIONS -lA'
# Some more alias to avoid making mistakes:
# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'
shopt -u -o history

在Python shell中:

import subprocess
proc = subprocess.Popen(['strings', '/path/to/.bashrc'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = proc.communicate()

第二个没有返回。为什么呢?

1 个答案:

答案 0 :(得分:2)

来自Travis Deployment official docs

  

shell参数(默认为False)指定是否将shell用作要执行的程序。如果shell为True,建议将args作为字符串而不是序列传递。

尝试使用:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context="YOUR_PROJECT.MainActivity">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

    <Button
        android:id="@+id/button_camera"
        android:text="Camera"
        android:layout_width="127dp"
        android:layout_height="106dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>
</RelativeLayout>
相关问题