【发布时间】:2014-03-05 21:00:59
【问题描述】:
我正在尝试运行脚本并返回标准输出(输出)。
调用脚本的代码是:
def read_wl_file(self, wl_file, script):
p = subprocess.Popen([script, wl_file], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return p.stdout.read()
脚本在下面,唯一的 arg 是要处理的文件
#!/bin/bash
usage(){
echo "Usage: $0 processes the thingy file."
exit 1 # error
}
# no arg, error
is_file_exits(){
local f="$1"
[[ -f "$f" ]] && return 0 || return 1
}
# invoke usage if filename not supplied
[[ $# -eq 0 ]] && usage
# Invoke is_file_exits
if ( is_file_exits "$1" )
then
#parse file code here....
else
echo "File not found"
fi
我可以毫无问题地从我的 shell 运行它,但不能在 python 脚本中运行,因为我的单元测试失败了。
def test_can_decypt(self):
output = self.data.read_wl_file('/Users/Bryan/Desktop/data.csv', "./xxx/script.sh")
print output
assert "Usage: ./xxx/script.sh processes thingy file." not in output
结果:
Usage: ./xxx/script.sh processes thingy file.
F.
======================================================================
FAIL: test_can_work (tests.file_tests.TestProcess)
----------------------------------------------------------------------
Traceback (most recent call last):
File ".....test.py", line 17, in test_can_work
assert "Usage: ./xxx/script.sh processes the thingy file." not in output
AssertionError
----------------------------------------------------------------------
Ran 2 tests in 0.006s
所以测试告诉我,当 Popen 运行时它没有传入 arg,所以用法是错误的输出(因此测试失败)。我在这一点上迷路了。代码看到脚本,打开它但没有传递任何参数?
【问题讨论】: