简体   繁体   English

带有args的Python subprocess.call

[英]Python subprocess.call with args

I'm trying to use python subprocess.call with args to call a shell script. 我正在尝试使用带有args的python subprocess.call来调用shell脚本。 The args I have aren't being passed to the shell script, but the script is being called no problem. 我的args没有被传递给shell脚本,但脚本被调用没有问题。 Here's what I've got 这就是我所拥有的

prepend = str(prepend)
print "prepend = " + str(prepend)
filename = str(request.FILES['mdbfile'])
print "filename = " + str(filename)
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
print "PROJECT_ROOT = " + str(PROJECT_ROOT)
filename = str(PROJECT_ROOT) + '/%s' % filename
print "full_filename = " + str(filename)
cmd = '%s/buildcsvs.sh' % (PROJECT_ROOT)
print "full_cmd = " + str(cmd)
p = subprocess.call([cmd, filename, prepend], shell=True)
output = p.stdout.read()
print output

Here's what the shell script looks like 这是shell脚本的样子

#${1} is the file name, ${2} is the prepend code
echo "mdb-export ${1} TEAM > \"${2}team.csv\""
mdb-export ${1} TEAM > "${2}team.csv"

And here's what the output looks like 这是输出的样子

prepend = 749176818
filename = 2011ROXBURY.mdb
PROJECT_ROOT = /Planner
full_filename = /Planner/2011ROXBURY.mdb
full_cmd = /Planner/buildcsvs.sh
Exception AttributeError: AttributeError("'_DummyThread' object has no attribute   '_Thread__block'",) in <module 'threading' from '/usr/lib/python2.7/threading.pyc'> ignored
mdb-export  TEAM > "team.csv"
Usage: mdb-export [options] <file> <table>

Does anyone have any idea what I'm doing wrong? 有谁知道我做错了什么? Thank you -- I appreciate your help 谢谢 - 谢谢你的帮助

EDIT: Right now, I have this: 编辑:现在,我有这个:

print "full_cmd = " + str(cmd)
args = "%s %s" % (filename, prepend)
print "full_args = " + str(args)
(out, err) = Popen(cmd,  stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True).communicate(args)

and it doesn't look like it's successfully completing the call to the script. 并且看起来它没有成功完成对脚本的调用。

Do you have any idea why? 你知道为什么吗?

If you pass shell=True args must be a string and not a list: 如果你传递shell=True args必须是一个字符串而不是一个列表:

In [4]: from subprocess import check_output

In [5]: check_output(['echo', '123'])
Out[5]: '123\n'

In [6]: check_output(['echo', '123'], shell=True)
Out[6]: '\n'

In [7]: check_output('echo 123', shell=True)
Out[7]: '123\n'

Edit: Instead of using call and p.stdout.read you should use Popen().communicate , that was made for this purpose and it helps to avoid deadlocks. 编辑:而不是使用callp.stdout.read你应该使用p.stdout.read Popen().communicate ,这是为了这个目的,它有助于避免死锁。

Edit² (answer to edit above): 编辑²(上面的编辑答案):

cmd = ' '.join([cmd, args])
(out, err) = Popen(cmd,  stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True, shell=True).communicate(None)

You have to pass the full commandline to Popen . 你必须将完整的命令行传递给Popen The Argument to communicate will be written into the process.stdin ( process = what Popen returns). communicate的参数将写入process.stdinprocess = Popen返回的内容)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM