2014-05-03 37 views
1

我试着从我的Python调用的shell脚本参数,但参数没有被传递Python的子POPEN传递参数

我的shell:

echo "Inside shell" 
echo $0 
echo $1 
cd $1 
pwd 
for file in *.csv 
do 
    split -l 50000 -d -a 4 "$file" "$file" 
done 
echo "Outside shell" 

与壳=真

this_dir = os.path.dirname(os.path.abspath(__file__)) 
cmd = [os.path.join(this_dir,'split.sh'),fileslocation] 
print 'cmd = ', cmd 
process = subprocess.Popen(cmd,shell=True) 

参数未正确传递...

with Shell = True删除

cmd = ['/opt/sw/p3/src/PricesPaidAPI/split.sh', '../cookedData'] 
Traceback (most recent call last): 
    File "csv_rename.py", line 23, in <module> 
    process = subprocess.Popen(cmd) 
    File "/usr/lib/python2.7/subprocess.py", line 679, in __init__ 
    errread, errwrite) 
    File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child 
    raise child_exception 
OSError: [Errno 8] Exec format error 
+1

[Python raise child \ _exception OSError:\ [Errno 8 \] Exec format error]的可能重复(http://stackoverflow.com/questions/16925909/python-raise-child-exception-oserror-errno-8 -exec-format-error)(你的shell脚本缺少shebang ...) – geoffspear

+0

@Wooble我不会说你提供的链接回答这个问题。它清楚地表明OP必须重新编译他的程序,而且它不是一个脚本。 Shebang只是一个没有得到证实的建议。虽然它可能是对的。 – luk32

+0

接受的答案给出了导致OP错误的两种可能性,并且从他在问题中粘贴的脚本中,您可以看到没有shebang线。 – geoffspear

回答

0

好吧,我不知道这应该是一个骗局,或不(如每评论)。但这是shebang帮助的事实。这里是确认。

popen_test.py:

import subprocess 
subprocess.Popen(["./dummy.sh", "test"]) 

noshebang.sh:

echo "Echoing: $1" 

这导致与OSError: [Errno 8] Exec format error。这是因为操作系统期望该文件是可执行的(杜)。然而,- 第一行中的shebang是系统的特殊标志,该文件应该在它指定的shell环境中执行。所以它可以被视为一个可执行文件,即使它不是。所以:

shebang.sh:

#!/bin/sh 

echo "Echoing: $1" 

作品。

它与python的shell=True基本相同。只是系统照顾它。 IMO用这种方法注入随机代码有点困难。所以我会建议去做。