- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我一直在写一些 python 代码,在我的代码中我使用的是“命令”
代码按我的预期工作,但后来我在 Python 文档中注意到该命令已被弃用,并将在 Python 3 中删除,我应该改用“subprocess”。
“好吧”我想,“我不希望我的代码直接进入遗留状态,所以我应该立即改变它。
问题是 subprocess.Popen
似乎在任何输出的开头添加了一个讨厌的字符串,例如
<subprocess.Popen object at 0xb7394c8c>
我看到的所有例子都有它,它似乎被认为是一直存在的。
这段代码;
#!/usr/bin/python
import subprocess
output = subprocess.Popen("ls -al", shell=True)
print output
产生这个;
<subprocess.Popen object at 0xb734b26c>
brettg@underworld:~/dev$ total 52
drwxr-xr-x 3 brettg brettg 4096 2011-05-27 12:38 .
drwxr-xr-x 21 brettg brettg 4096 2011-05-24 17:40 ..
<trunc>
这正常吗?如果我将它用作向控制台输出各种格式化详细信息的更大程序的一部分,它会把一切都搞砸。
我正在使用命令获取接口(interface)的 IP 地址,方法是使用 ifconfig 以及各种 greps 和 awk 来抓取地址。
考虑这段代码;
#!/usr/bin/python
import commands,subprocess
def new_get_ip (netif):
address = subprocess.Popen("/sbin/ifconfig " + netif + " | grep inet | grep -v inet6 | awk '{print $2}' | sed 's/addr://'i", shell=True)
return address
def old_get_ip (netif):
address = commands.getoutput("/sbin/ifconfig " + netif + " | grep inet | grep -v inet6 | awk '{print $2}' | sed 's/addr://'i")
return address
print "OLD IP is :",old_get_ip("eth0")
print ""
print "NEW IP is :",new_get_ip("eth0")
返回;
brettg@underworld:~/dev$ ./IPAddress.py
OLD IP is : 10.48.16.60
NEW IP is : <subprocess.Popen object at 0xb744270c>
brettg@underworld:~/dev$ 10.48.16.60
至少可以说这很糟糕。
显然我在这里遗漏了一些东西。我当然是 Python 的新手,所以我确定是我做错了事,但到目前为止,各种谷歌搜索都没有结果。
如果我想要更干净的输出怎么办?我是否必须手动修剪有问题的输出,或者我是否错误地调用了 subprocess.Popen
?
最佳答案
“丑陋的字符串”是它应该打印的内容。 Python 正确打印出 repr(subprocess.Popen(...))
, 就像你说 print(open('myfile.txt'))
时它会打印的一样.
此外,python 不知道输出到标准输出的是什么。您看到的输出不是 来自 python,而是来自进程的 stdout 和 stderr 作为垃圾邮件被重定向到您的终端,甚至没有经过 python 进程。这就像你运行了一个程序 someprogram &
没有将其标准输出和标准错误重定向到 /dev/null
,然后尝试运行另一个命令,但您偶尔会看到来自该程序的垃圾邮件。重复并澄清:
<subprocess.Popen object at 0xb734b26c> <-- output of python program
brettg@underworld:~/dev$ total 52 <-- spam from your shell, not from python
drwxr-xr-x 3 brettg brettg 4096 2011-05-27 12:38 . <-- spam from your shell, not from python
drwxr-xr-x 21 brettg brettg 4096 2011-05-24 17:40 .. <-- spam from your shell, not from python
...
为了捕获标准输出,您必须使用 .communicate()
函数,像这样:
#!/usr/bin/python
import subprocess
output = subprocess.Popen(["ls", "-a", "-l"], stdout=subprocess.PIPE).communicate()[0]
print output
此外,您永远不想使用 shell=True
,因为它是一个安全漏洞(一个具有未过滤输入的主要安全漏洞,一个没有输入的小安全漏洞,因为它允许通过修改 shell 环境进行本地攻击)。出于安全原因以及为了避免错误,您通常希望传入列表而不是字符串。如果你懒惰,你可以执行“ls -al”.split(),这是不受欢迎的,但执行类似 (“ls -l %s”%unsanitizedInput).split() 之类的操作将是一个安全漏洞。
关于python - subprocess.Popen 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6147193/
我正在使用java引擎来处理一些内容。 我想知道这样会占用不必要的资源吗? command = 'some command to run my jar file' p = subprocess.Pop
我正在尝试使用以下代码块将一个进程的输出作为输入发送到另一个进程: p1 = Popen(command, stdout=PIPE) p2 = Popen(dgxmcmd, stdin=p1.stdo
popen 缓冲输出而系统没有。这是唯一的区别吗? 我知道 popen 和 system 都通过 shell 运行命令。但是,popen() 是否为 evil作为系统()? 最佳答案 看,从本质上讲,
用代码最容易解释: require 'timeout' puts "this block will properly kill the sleep after a second" IO.popen("
似乎既执行子进程又创建管道进行输入/输出,只是 subprocess 较新。 我的问题是,有没有subprocess.Popen可以做而os.popen不能做的功能,所以我们需要新模块subproce
我有一个生成以下输出的程序: ┌───────────────────────┐ │10 day weather forecast│
我正在使用以下命令来运行 shell 命令(创建子进程): cmd = "ls" process = subprocess.Popen(cmd, shell=True, stdout=subproce
得到结果后,我需要停止通过Python中的Popen发出的服务(在另一个线程的后台运行),但是以下方法失败了(只是使用ping)解释): class sample(threading.Thread):
Python - os.popen 和 subprocess.Popen 有什么区别? 最佳答案 os 进程功能被认为已过时。 subprocess 模块是在 Python 2.4 中引入的,作为与子
根据 python 文档 http://docs.python.org/library/subprocess.html ,建议将 os.popen 替换为 Popen 类,现在我有以下命令: impo
非常具体的问题(我希望):以下三个代码有什么区别? (我希望它只是第一个不等待子进程完成,而第二个和第三个会这样做。但我需要确定这是 only 的区别...) 我也欢迎其他评论/建议(尽管我已经很清楚
我有以下代码: pwd = '/home/user/svnexport/Repo/' updateSVN = "svn up " + pwd cmd = os.popen(updateSVN) get
我正在尝试编写简单的 c 函数,这将使我有可能看到从一个流到另一个流的数据传输进度,并将其显示在我的字符 LCD 上。 我设法传输数据并指示进度,但如何获得管道的结果? 所以基本上我想在 c 中做对应
我正在尝试使用 subprocess 模块与使用 Python 的命令行聊天机器人进行通信。 (http://howie.sourceforge.net/使用编译后的 win32 二进制文件,我有我的
我需要为 Ghostscript 创建一个 Monkey 补丁,我必须从 os.popen 迁移到 subsession.popen 因为我不能在我的系统中使用 shell . 我这样试过: def
在 Linux 操作系统上,下面的 python 代码提供了当前目录中的目录。 dirs = os.popen('ls -d */').read().split(os.linesep) print d
当我们从 Python 2.7.3 升级到 Python 2.7.5 时,大量使用 subprocess.Popen() 的内部库的自动化测试开始失败。该库用于线程环境。调试问题后,我能够创建一个简短
我无法得到它与 bash 相关或 python 子进程,但结果不同: >>> subprocess.Popen("echo $HOME", shell=True, stdout=subprocess.
这里我想执行一个命令,我必须在执行第一个命令后给这个命令输入。 我想执行 obex_test蓝牙模式命令而不是在我必须为启动服务器提供像's'这样的输入之后我怎么能给这个东西。这是我的代码,我写了这个
在我的 Lua 程序中,我必须捕获来自外部程序的输出。这个外部程序需要某些环境变量。所以我这样做: e = "" e = e .. "A=100;" e = e .. "B=Hi;" e = e ..
我是一名优秀的程序员,十分优秀!